简体   繁体   中英

how to make value dynamic while copying data from one workbook to another workbook

I have to copy the data of product category A01 to another workbook having a sheet named A01. but the product categories have A011,A012,A013 and so on data also. so, for that I want to make it dynamics.

I tried:

Sub left()
vcounter = Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(Rows.count, 1).End(xlUp).row
For k = 1 To 9
For j = 2 To vcounter
    If Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(j, 3).Value = "A01" & k Then
        Workbooks("macro tool.xlsm").Worksheets("sheet1").Rows(j).Copy
        Workbooks("A.xlsx").Worksheets("A01").Activate
        vcounter1 = Workbooks("A.xlsx").Worksheets("A01").Cells(Rows.count, 1).End(xlUp).row
        Workbooks("A.xlsx").Worksheets("A01").Cells(vcounter1 + 1, 1).Select
        ActiveSheet.PasteSpecial
    End If
Next j
Next k
End Sub

the output of this program is:

A011
A011
A011
A011
A012
A012
A012
A012
A012
A012
A012
A012
A013
A013
A013
A013
A013
A013
A013
A013
A013
A014
A014
A014
A015
A015
A015
A015
A015
A015
A016
A016
A016
A016
A016
A016
A016
A016
A017
A017
A017
A017
A017
A017
A018
A018
A018
A018
A018
A018
A018
A019
A019
A019
A019
A019

and not giving the values for

A01

I wrote the following code:

Sub left()
vcounter = Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(Rows.count, 1).End(xlUp).row

For j = 2 To vcounter
    If Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(j, 3).Value = left(Range("C" & j), 3) = "A01" Then
        Workbooks("macro tool.xlsm").Worksheets("sheet1").Rows(j).Copy
        Workbooks("A.xlsx").Worksheets("A01").Activate
        vcounter1 = Workbooks("A.xlsx").Worksheets("A01").Cells(Rows.count, 1).End(xlUp).row
        Workbooks("A.xlsx").Worksheets("A01").Cells(vcounter1 + 1, 1).Select
        ActiveSheet.PasteSpecial
    End If
Next j
End Sub

but this is not working as I used the left function with value.

I want both the values having product code A01 as well as other values, thats why I tried to use left function but the code isn't working. it is not giving any error but also not giving any output too.

Sub left() vcounter = Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(Rows.count, 1).End(xlUp).row For k = 1 To 9 Workbooks("macro tool.xlsm").Worksheets("sheet1").Rows(1).Copy Workbooks("A.xlsx").Worksheets("A0" & k).Rows(1) For j = 2 To vcounter If left(Workbooks("macro tool.xlsm").Worksheets("sheet1").Range("C" & j), 3) = "A0" & k Then Workbooks("macro tool.xlsm").Worksheets("sheet1").Rows(j).Copy Workbooks("A.xlsx").Worksheets("A0" & k).Activate vcounter1 = Workbooks("A.xlsx").Worksheets("A0" & k).Cells(Rows.count, 1).End(xlUp).row Workbooks("A.xlsx").Worksheets("A0" & k).Cells(vcounter1 + 1, 1).Select ActiveSheet.PasteSpecial End If Next j Next k End Sub

Try

If left(Workbooks("macro tool.xlsm").Worksheets("sheet1").Cells(j, 3).Value,3) = left(Range("C" & j), 3) = "A01" Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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