Excel中如何批量重命名工作表与将每个工作表导出到单独Excel文件
2023-06-13 09:15:29 时间
Excel中通过VBA批量重命名工作表Worksheet
Step 1: 打开Developer Tab找到VBA (快捷键 Alt+F11)
Step 2: Insert –>Module
Step 3:
将以下代码复制进去
Sub RenameSheet()
Dim rs As Worksheet
For Each rs In Sheets
rs.Name = rs.Range("B5")
Next rs
End Sub
Step 4: 按F5运行,或关闭VBA后,通过 Excel View –>Macros –>View Macros–>Run
如果指定单元格没有数据怎么办? 我们可以添加一个条件进去即可。
Sub RenameSheet()
Dim rs As Worksheet
For Each rs In Sheets
If rs.Range("F3").Value <> "" Then
rs.Name = rs.Range("F3")
End If
Next rs
End Sub
Excel中通过VBA批量修改特定位置颜色
单个无条件修改全部工作表
Sub BackGroudColor()
Dim rs2 As Worksheet
For Each rs2 In Sheets
rs2.Range("C6").Interior.Color = RGB(180, 198, 231)
rs2.Range("B7").Interior.Color = RGB(255, 230, 153)
rs2.Range("E6").Interior.Color = RGB(198, 224, 180)
Next rs2
End Sub
有条件修改目前工作表
Sub Fill_Cell_Condition()
Dim rngCell As Range
For Each rngCell In Range("A6:A19")
If Len(rngCell.Value) <> "0" Then
rngCell.Cells.Interior.Color = RGB(255, 230, 153)
'If Everything in A6-A19 The length of the cell value is not zero, change backgroud color. Otherwise, do nothing
End If
Next rngCell
有条件修改全部工作表
修改全部工作表的代码为
Dim ws As Worksheet
For Each ws In Sheets
###在中间插入你想要全部工作表都修改的代码
Next ws
========================================
Sub Fill_Cell_Condition()
Dim wsFill As Worksheet
Dim i
For Each wsFill In Sheets
For i = 8 To 20
If wsFill.Cells(i, 1).Value <> "" Then
'当A8-A20里不是没有值,则着色
wsFill.Cells(i, 1).Interior.Color = RGB(155, 30, 153)
End If
Next
Next wsFill
End Sub
根据Excel特定列分成不同工作表 - Excel VBA
这个在之后的文章有提及,大家可以参考一下。
</2021/01/18/2021-01-18-Split-excel-data-into-sheets-by-column-values/>
将每个工作表导出到单独Excel文件 - Excel VBA
Step 1: Hold down the ALT + F11 keys in Excel, and it opens the Microsoft Visual Basic for Applications window
Step 2: Click Insert > Module, and paste the following code in the Module Window
Step 3: 复制以下代码
Sub Splitbook()
'Updateby20140612
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
将每个工作表导出到单独Excel文件 - Python
Step 1: 在Termianl选择pip install组件pypiwin32
pip install pypiwin32
Step 2: 使用pycharm并填写代码
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def create_wb_from_ws():
try:
filepath = 'D:\sp\test.xlsx'
from win32com.client import DispatchEx
excel = DispatchEx("Excel.Application")
if excel == None:
print('-' * 100)
print('Error: Excel is not found on this machine. Existing!')
print('-' * 100)
return
else:
print('-' * 100)
print('Message: Excel version {0} is available.'.format(excel.version))
print('-' * 100)
if int(float(excel.version)) < 12:
fileext = '.xls'
else:
fileext = '.xlsx'
import os
if not os.path.exists(filepath):
print('The entered file path does not exists. Existing!')
return
filedir = os.path.join(os.path.dirname(filepath), os.path.splitext(os.path.basename(filepath))[0])
if not os.path.exists(filedir):
os.mkdir(filedir)
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(Filename=filepath)
wb.Application.Visible = False
for sheet in wb.Worksheets:
filename = os.path.join(filedir, sheet.name + fileext)
wbnew = excel.Workbooks.Add()
wbnew.Application.Visible = False
sheet.Copy(Before=wbnew.Worksheets(1))
for s in wbnew.Worksheets:
if s.name != sheet.name:
wbnew.Worksheets(s.name).Delete()
wbnew.SaveAs(filename)
print('Saved sheet name "{0}" as a new excel file at {1}'.format(sheet.name, filename))
wbnew.Close(SaveChanges=1)
wb.Close(True)
excel.Quit()
except:
print('-' * 100)
print('Error occurred')
print('-' * 100)
raise
if __name__ == "__main__":
create_wb_from_ws()
将Excel特定列直接分成单独文件 - Python
这个在之后的文章有提及,大家可以参考一下。
</2021/01/18/2021-01-18-Split-excel-data-into-sheets-by-column-values/>
相关文章
- 导出的Excel名字乱码_恢复的excel文件乱码
- EasyExcel实现Excel文件导入
- matlab批量处理excel(CSV)文件数据
- 利用模板导出文件(一)之XLSTransformer导出excel文件
- hutool excel写数据
- 前端必读3.0:如何在 Angular 中使用SpreadJS实现导入和导出 Excel 文件
- Excel学习: 文件操作之---复制模板按面试分组名重命名
- excel 多列内容拼接
- 使用EasyExce实现Excel文件解析
- 图片链接如何在excel里转成图片_mdf文件怎么转成Excel
- 如何利用python读excel数据_python在excel应用实例
- 100000行级别数据的Excel导入优化之路
- JAVA以UTF-8导出CSV文件,用excel打开产生乱码的解决方法
- java使用poi读取excel文件代码示例详解编程语言
- MySQL导入Excel文件的快捷方法(mysql导入xls文件)
- [问题解决]大数据量上载excel文件数据到SAP系统[ALSM_EXCEL_TO_INTERNAL_TABLE]详解编程语言
- 文件Linux下如何打开Excel文件(linux打开excel)
- 轻松实现Excel导入MySQL数据库(excel导入mysql数据库)
- 如何使用Excel连接MySQL数据库(excel连接mysql数据库)
- MySQL读取Excel文件的简单方法(mysql读取excel)
- Excel与Oracle无缝连接,极致解决数据融合问题(excel连oracle)
- 通过table标签,PHP输出EXCEL的实现方法
- Python读写Excel文件的实例
- java使用poi读取ppt文件和poi读取excel、word示例