简体   繁体   English

我如何 select 基于单元格值的列范围?

[英]How do i select a range of columns based on cell values?

I'm trying to select and copy the range of columns in workbook 2 based on the values in cell C2 and cell C3 from workbook 1.我正在尝试 select 并根据工作簿 1 中单元格 C2 和单元格 C3 中的值复制工作簿 2 中的列范围。

Sub copy_data1()

Dim wbmain_name, wb1_name As Variant
Dim wb1_ws1_name As Variant
Dim start_columnletter As String
Dim end_columnletter As String
Dim sheet_1 As Worksheet
Dim sheet_2 As Worksheet

wbmain_name = Range("C5")
wb1_name = Range("B9") & Range("B10")
wb1_ws1_name = Range("C10")

'Settings.
Set sheet_1 = Workbooks(wb1_name).Sheets(wb1_ws1_name) '‹ Specify here the source sheet.
Set sheet_2 = Workbooks(wbmain_name).Sheets(wb1_ws1_name) '‹ Specify here the destination sheet.
start_columnletter = Range("C2").Value
end_columnletter = Range("C3").Value


'Copying.
sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)

End Sub结束子

If you want to select a whole column try: Range("AS:AS").如果你想 select 一整列试试: Range("AS:AS").

It's pretty simple to solve your problem.解决您的问题非常简单。 If you want the column number:如果你想要列号:

Sub a()  
    
    Dim r As Range  
    Dim colNo As Integer  
    Set r = Range("AS:AS")  
    colNo = r.Column  
    MsgBox colNo  

End Sub

Long time not working with VBA, hope it does not go wrong.好久没用 VBA,希望 go 不会出错。

Try this code:试试这个代码:

Sub SubCopy()
    
    'Declarations.
    Dim start_columnletter As String
    Dim end_columnletter As String
    Dim sheet_1 As Worksheet
    Dim sheet_2 As Worksheet
    
    'Settings.
    Set sheet_1 = Workbooks("workbook 1.xlsm").Sheets(1) '‹ Specify here the source sheet.
    Set sheet_2 = Workbooks("workbook 2.xlsm").Sheets(1) '‹ Specify here the destination sheet.
    start_columnletter = sheet_1.Range("C2").Value
    end_columnletter = sheet_1.Range("C3").Value
    
    'Copying.
    sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)
    
End Sub



About your original code:关于您的原始代码:

    'get column letter
    start_columnletter = Range("C2").Value '‹ This will refer to the range C2 of the active sheet. It might not be the one you want.
    end_columnletter = Range("C3").Value '‹ This will refer to the range C2 of the active sheet. It might not be the one you want.
    
    'get column number based on column letter
    start_columnnumber = Range(start_columnletter & 1).Column '‹ This is not really necessary since you have start_columnletter already. Also it will refer to the active sheet.
    end_columnnumber = Range(end_columnletter & 1).Column '‹ This is not really necessary since you have end_columnletter already. Also it will refer to the active sheet.
    
    Workbook("workbook2").Range(Columns(start_columnnumber), Columns(end_columnnumber)).Select
    'The line up here won't work because:
    '-Workbook("workbook2") does not exist; Workbooks("workbook2") might be the one you are looking for;
    '-Workbook("workbook2").Range does not exist; Workbooks("workbook2").Sheets(1).Range might be the one you are looking for;
    '-the Select method will select the range; it will not copy it; avoid selecting objects as much as possible;
    '-the Select method will not select the range this way unless the parent sheet (and of course the parent workbook) is already selected; such might not be the case already.



The "subscript out of range" error might be caused by a wrong reference in the name of the workbooks. “下标超出范围”错误可能是由于工作簿名称中的错误引用引起的。 Make sure they are correctly reported.确保它们被正确报告。 Maybe a space shouldn't be there or the file extension in not correct (.xls.xlsx.xlsm).也许不应该有空格或文件扩展名不正确(.xls.xlsx.xlsm)。 About the last code you posted:关于您发布的最后一个代码:

Sub copy_data1()
    
    Dim wbmain_name, wb1_name As Variant '‹ I'd suggest to set them as String.
    Dim wb1_ws1_name As Variant  '‹ I'd suggest to set it as String.
    Dim start_columnletter As String
    Dim end_columnletter As String
    Dim sheet_1 As Worksheet
    Dim sheet_2 As Worksheet
    
    wbmain_name = Range("C5")  '‹ I'd suggest to set is as Range("C5").Value ; what's in range C5? It also refers to the active sheet of the active workbook.
    wb1_name = Range("B9") & Range("B10")  '‹ Same as wbmain_name comment.
    wb1_ws1_name = Range("C10") '‹ Same as wbmain_name comment.
    
    'Settings.
    Set sheet_1 = Workbooks(wb1_name).Sheets(wb1_ws1_name)
    Set sheet_2 = Workbooks(wbmain_name).Sheets(wb1_ws1_name)
    start_columnletter = Range("C2").Value '‹ You should refer to sheet_1.
    end_columnletter = Range("C3").Value '‹ You should refer to sheet_1.
    
    
    'Copying.
    sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)
    
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 select 我活动范围内的最后一个单元格? - How do I select the last cell in my active range? 如何选择所选单元格下方的范围? - How do I select a range below a selected cell? 如何根据范围中的值填充单元格? - How do I populate a cell based on its value from a range? 如何根据另一个单元格的值自动为一系列单元格填充值? 有Excel等同于GoogleSheet的arrayformula吗? - How do I autofill a range of cells with values, based on another cell's value? Is there an Excel's equivalent of GoogleSheet's arrayformula? 使用VBA根据单元格值在Excel中隐藏列范围 - Hiding Range of Columns in Excel using VBA based on cell Values 根据 header 和前面的列设置单元格范围值 - Setting cell range values based on header and preceding columns 如何根据列名而不使用范围(“ A”)来选择列? - How do I select a column based on its name and not using range(“A”)? 如何根据单元格内容选择多个工作表进行移动? - How do I select multiple worksheets to move based on cell content? 如何基于3列的值有条件地格式化范围 - How to conditional format a range based on the values of 3 columns VBA 如何根据该范围内一个单元格的值有条件地格式化一系列单元格? - VBA How do I conditionally format a range of cells based on the value of one cell in that range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM