简体   繁体   English

根据单元格值复制单元格

[英]Copy cells based on cell value

I have a spreadseet with quite a few refrences (lookups and links to data I extract from system) In cell B1 I have how many actually rows with data I have.我有一个带有很多引用的电子表格(查找和链接到我从系统中提取的数据)在单元格 B1 中,我有多少实际行的数据。 ie The sheet is called Raw Data and if B1=100 I need range B2:E102 copied into sheet Master The value in B1 is dynamic, depending on data in another sheet).即工作表称为原始数据,如果 B1=100,我需要将范围 B2:E102 复制到工作表 Master 中 B1 中的值是动态的,具体取决于另一张工作表中的数据)。 Could someone please help me with this?有人可以帮我解决这个问题吗? Thanks谢谢

Copy a Range复制范围

Option Explicit

Sub CopyRange()
    
    ' Reference the workbook.
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Reference the Source range.
    Dim sws As Worksheet: Set sws = wb.Sheets("Raw Data")
    Dim slRow As Long: slRow = sws.Range("B1").Value + 2
    Dim srg As Range: Set srg = sws.Range("B2", sws.Cells(slRow, "E"))
    
    ' Reference the first Destination cell.
    Dim dws As Worksheet: Set dws = wb.Sheets("Master")
    Dim dfCell As Range: Set dfCell = dws.Range("A1") ' adjust!?
    
    ' Either copy values, formulas and formats,...
    srg.Copy dfCell
    ' or copy only values (more efficient):
    'dfCell.Resize(srg.Rows.Count, srg.Columns.Count).Value = srg.Value
    
    ' Inform.
    MsgBox "Range copied.", vbInformation
       
End Sub

I like to create a hard coded starting point for the Range.我喜欢为 Range 创建一个硬编码的起点。

Then from there step into getting the last column then the last row.然后从那里开始获取最后一列,然后是最后一行。

Something like this:是这样的:

Sub Main()

strFile = "C:\Users\raw_data.xlsm" 'change this to your file name
Workbooks.Open (strFile)
'Debug.Print strFile

'log the last column for paramters
LastColumn = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

'log the last rows for components
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

Dim WorksheetStartCell As String

StartCellNum = 2

WorksheetHeadingStartCell = "A" & StartCellNum
WorksheetValueStartCell = "A" & StartCellNum + 1

'Debug.Print "Worksheet Heading Start Cell: " & WorksheetHeadingStartCell
'Debug.Print "Worksheet Value Start Cell: " & WorksheetValueStartCell

WorksheetHeadingEndCell = "G" & StartCellNum
WorksheetValueEndCell = "G" & LastRow

End Sub

That will dynamically give you everything aside from it finding the very first cell of the range, which is easy enough but like you said, you had it hard coded to "B2" so I thought it was relevant.除了找到范围的第一个单元格之外,这将动态地为您提供所有内容,这很容易,但就像您说的那样,您已将其硬编码为“B2”,所以我认为它是相关的。

Once you have the range that you actually want like written above, you can easily copy and paste the range to that sheet.一旦你有了上面写的你真正想要的范围,你就可以轻松地将范围复制并粘贴到该工作表。

I assumed you needed more help with the dynamic range than the copying and pasting of the range, if that was not the case, I can reply again to that concern.我假设您在动态范围方面需要更多帮助,而不是复制和粘贴范围,如果情况并非如此,我可以再次回复该问题。

I have not replied to many answers on here so sorry if I was not much help.我没有在这里回复很多答案,如果我没有太大帮助,我很抱歉。 I did try my best to address your concerns.我确实尽力解决了您的顾虑。 I truly wish this helps you.我真的希望这对你有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM