Vba “连接三段代码”;“打开对话框”&引用;剪贴;及;拆分列重命名";
我希望你能帮忙。我有以下三段代码。这三种方式彼此完全独立。所有编译宏的操作都无法正确执行 第一段代码Vba “连接三段代码”;“打开对话框”&引用;剪贴;及;拆分列重命名";,vba,excel,copy-paste,Vba,Excel,Copy Paste,我希望你能帮忙。我有以下三段代码。这三种方式彼此完全独立。所有编译宏的操作都无法正确执行 第一段代码子打开工作簿对话框()打开一个对话框,允许用户选择文件 第二段代码Public Sub-Sample()在列标题中搜索文本“CountryCode”,然后将该列剪切并粘贴到列F中 第三段代码Public Sub Filter()将F列拆分为新的工作表,并根据国家/地区重命名工作表 因此,宏基本上要做的是打开一个对话框,获取文件,找到所在的国家/地区列,将其剪切并粘贴到F列,然后将该列拆分为新的工作
子打开工作簿对话框()
打开一个对话框,允许用户选择文件
第二段代码Public Sub-Sample()
在列标题中搜索文本“CountryCode”,然后将该列剪切并粘贴到列F中
第三段代码Public Sub Filter()
将F列拆分为新的工作表,并根据国家/地区重命名工作表
因此,宏基本上要做的是打开一个对话框,获取文件,找到所在的国家/地区列,将其剪切并粘贴到F列,然后将该列拆分为新的工作表并重命名
就像我说的,所有代码都可以独立工作,但是当我把它们放在一起时。对话框打开,我选择了我的文件,然后我得到了Msgbox“Country not Found”,即使CountryCode列在范围内,我想Set aCell=.range(“A1:X50”)
CountryCode在W列中
单击MsgBox“未找到国家/地区”后,将执行公共子筛选器()
并拆分和重命名错误的列。该发现似乎没有发生,因此剪切和粘贴没有发生
为了更好地理解,我附上了一些图片
找不到国家/地区
被错误的F分开
代码如下
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection
If my_FileName <> False Then
Workbooks.Open Filename:=my_FileName
Call Sample '<--|Calls the Filter Code and executes
Call Filter '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Sample()
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
'~~> Cut the entire column
aCell.EntireColumn.Cut
'~~> Insert the column here
Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub
Public Sub Filter()
Dim rCountry As Range, helpCol As Range
With Worksheets("Sheet1") '<--| refer to data worksheet
With .UsedRange
Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in
End With
With .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A"
.Columns(6).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column
Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row)
For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row)
.AutoFilter 6, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered...
Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet
ActiveSheet.Name = rCountry.Value2 '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
End If
Next
End With
.AutoFilterMode = False '<--| remove autofilter and show all rows back
End With
helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included)
End Sub
子打开工作簿对话框()
将my_文件名设置为变体
MsgBox“选择您的TOV文件”剪切整个列
全部切除
“~~>在此处插入列
列(“F:F”)。插入移位:=xlToRight
其他的
MsgBox“未找到国家/地区”
如果结束
以
端接头
公共子筛选器()
Dim R国家作为范围,helpCol作为范围
对于工作表(“Sheet1”)”而言,问题在于您没有在打开的工作簿中搜索“CountryCode”,而是在运行代码的工作簿中搜索。因此,基本上您有一个工作簿,在其中启动宏代码并打开另一个要使用的工作簿(通过使用对话框)。但是在您的Public子示例()
中,您的问题是:
Set ws = ThisWorkbook.Sheets("Sheet1")
问题是您正在引用工作簿,在该工作簿中使用thiswoolk
编写和执行宏代码。因为您不知道Public子示例()
中的文件名。我已编辑了您的代码,使其能够正常工作:
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
Dim my_Workbook As Workbook
MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection
If my_FileName <> False Then
Set my_Workbook = Workbooks.Open(Filename:=my_FileName)
Call Sample(my_Workbook)'<--|Calls the Filter Code and executes
Call Filter(my_Workbook) '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Sample(my_Workbook as Workbook)
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = my_Workbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
'~~> Cut the entire column
aCell.EntireColumn.Cut
'~~> Insert the column here
Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub
Public Sub Filter(my_Workbook as Workbook)
Dim rCountry As Range, helpCol As Range
With my_Workbook.Worksheets("Sheet1") '<--| refer to data worksheet
With .UsedRange
Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in
End With
With .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A"
.Columns(6).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column
Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row)
For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row)
.AutoFilter 6, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered...
Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet
ActiveSheet.Name = rCountry.Value2 '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
End If
Next
End With
.AutoFilterMode = False '<--| remove autofilter and show all rows back
End With
helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included)
End Sub
子打开工作簿对话框()
将my_文件名设置为变体
将我的工作簿设置为工作簿
MsgBox“选择您的TOV文件”剪切整个列
全部切除
“~~>在此处插入列
列(“F:F”)。插入移位:=xlToRight
其他的
MsgBox“未找到国家/地区”
如果结束
以
端接头
公共子筛选器(my_工作簿作为工作簿)
Dim R国家作为范围,helpCol作为范围
使用my_Workbook.Worksheets(“Sheet1”)时,问题在于您没有在打开的工作簿中搜索“CountryCode”,而是在运行代码的工作簿中搜索。因此,基本上您有一个工作簿,在其中启动宏代码并打开另一个要使用的工作簿(通过使用对话框)。但是在您的Public子示例()
中,您的问题是:
Set ws = ThisWorkbook.Sheets("Sheet1")
问题是您正在引用工作簿,在该工作簿中使用thiswoolk
编写和执行宏代码。因为您不知道Public子示例()
中的文件名。我已编辑了您的代码,使其能够正常工作:
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
Dim my_Workbook As Workbook
MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection
If my_FileName <> False Then
Set my_Workbook = Workbooks.Open(Filename:=my_FileName)
Call Sample(my_Workbook)'<--|Calls the Filter Code and executes
Call Filter(my_Workbook) '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Sample(my_Workbook as Workbook)
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Change this to the relevant sheet
Set ws = my_Workbook.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
'~~> Cut the entire column
aCell.EntireColumn.Cut
'~~> Insert the column here
Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub
Public Sub Filter(my_Workbook as Workbook)
Dim rCountry As Range, helpCol As Range
With my_Workbook.Worksheets("Sheet1") '<--| refer to data worksheet
With .UsedRange
Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in
End With
With .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A"
.Columns(6).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column
Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row)
For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row)
.AutoFilter 6, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered...
Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet
ActiveSheet.Name = rCountry.Value2 '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
End If
Next
End With
.AutoFilterMode = False '<--| remove autofilter and show all rows back
End With
helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included)
End Sub
子打开工作簿对话框()
将my_文件名设置为变体
将我的工作簿设置为工作簿
MsgBox“选择您的TOV文件”剪切整个列
全部切除
“~~>在此处插入列
列(“F:F”)。插入移位:=xlToRight
其他的
MsgBox“未找到国家/地区”
如果结束
以
端接头
公共子筛选器(my_工作簿作为工作簿)
Dim R国家作为范围,helpCol作为范围
使用my_工作簿。工作表(“Sheet1”)在设置aCell变量时,包括行中包含CountryCodes(本例中为W列)的列。在设置aCell变量时,包括行中包含CountryCodes(本例中为W列)的列。这很可能是一个参考问题
简单地说,这意味着您没有传递新打开工作簿的引用,因此您的其他sub
不知道您在谈论哪一个
我举了一个例子,向您展示在何处进行更改:
Sub Open_Workbook_Dialog()
Dim my_FileName As Variant
'~~> Changes here
Dim MainWbk As Workbook
Dim OpenedWbk As Workbook
'~~> Changes here
Set MainWbk = ThisWorkbook
MsgBox "Pick your TOV file"
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")
If my_FileName <> False Then
'~~> Changes here
Set OpenedWbk = Workbooks.Open(Filename:=my_FileName)
'~~> Changes here
Call Sample(OpenedWbk, MainWbk)
''~~> Same changes to do here
'Call Filter
End If
End Sub
'~~> Changes here (arguments to pass the references of the workbooks)
Public Sub Sample(OpenedWbk As Workbook, MainWbk As Workbook)
Dim ws As Worksheet
Dim aCell As Range, Rng As Range
Dim col As Long, lRow As Long
Dim colName As String
'~~> Changes here
Set ws = OpenedWbk.Sheets("Sheet1")
With ws
Set aCell = .Range("A1:X50").Find(What:="CountryCode", _
LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.EntireColumn.Cut
'~~> Changes here
MainWbk.Columns("F:F").Insert Shift:=xlToRight
Else
MsgBox "Country Not Found"
End If
End With
End Sub
子打开工作簿对话框()
将my_文件名设置为变体
“~~>这里有变化
将MainWbk设置为工作簿
Dim OpenedWbk作为工作簿
“~~>这里有变化
设置MainWbk=此工作簿
MsgBox“选择您的TOV文件”
my_FileName=Application.GetOpenFilename(文件过滤器:=“Excel文件,*.xl*;*.xm*”)
如果我的文件名为False,那么
“~~>这里有变化
设置OpenedWbk=Workbooks.Open(文件名:=my_文件名)
“~~>这里有变化
调用示例(OpenedWbk、MainWbk)
“”~~>此处要做的更改相同
'呼叫过滤器
如果结束
端接头
“~~>此处的更改(传递工作簿引用的参数)
公共子示例(OpenedWbk作为工作簿,MainWbk作为工作簿)
将ws设置为工作表
调暗aCell作为范围,Rng作为范围
暗颜色和长颜色一样,浅颜色和长颜色一样
Dim colName作为字符串
“~~>这里有变化
设置ws=OpenedWbk.Sheets(“Sheet1”)
与ws
设置aCell=.Range(“A1:X50”).Find(What:=“CountryCode”_
LookIn:=xlValues,LookAt:=xlother_
MatchCase:=False,SearchFormat:=False)
如果不是的话,亚塞尔什么都不是
全部切除
“~~>这里有变化
MainWbk.Columns(“F:F”)。插入移位:=xlToRight
其他的
MsgBox“未找到国家/地区”
如果结束
以
端接头
<