I have 4 columns on Sheet1 and 4 on Sheet2
I have named the columnson sheet 1 as:
MvarNo = 1
Mdesc = 2
Mfile = 3
Mfield = 4
where 1 = a etc....
How do I name these columns with the sheet name and column number?
Thanks
UPDATE
Sub Compare()
MvarNo = 1 'Sheet1
Mdesc = 2 'Sheet1
Mfile = 3 'Sheet1
Mfield = 4 'Sheet1
XvarNo = 6 'Sheet1
Xdesc = 7 'Sheet1
Xfile = 8 'Sheet1
Xfield = 9 'Sheet1
CvarNo = 1 'Sheet2
Cdesc = 2 'Sheet2
Cfile = 3 'Sheet2
Cfield = 4 'Sheet2
'Count rows on Master Sheet (Sheet1)
MvarNoLastRow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
MdesclastRow = Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
MfilelastRow = Worksheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
MfieldlastRow = Worksheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
'Count rows on Compare Sheet (Sheet2)
CvarNoLastRow = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
CdesclastRow = Worksheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row
CfilelastRow = Worksheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Row
CfieldlastRow = Worksheets("Sheet2").Cells(Rows.Count, "D").End(xlUp).Row
For i = 2 To MvarNoLastRow
For j = 2 To CvarNoLastRow
If Cells(i, MvarNo) = Cells(j, CvarNo) Then
Cells(i, XvarNo).Value = "Yes"
' If Cells(i, MvarNo) = Cells(j, CvarNo) And Cells(i, Mdesc) = Cells(j, Cdesc) And Cells(i, Mfile) = Cells(j, Cfile) And Cells(i, Mfield) = Cells(j, Cfield) Then
' Cells(i, XvarNo).Value = "Yes"
' ElseIf Cells(i, MvarNo) <> Cells(j, CvarNo) And Cells(i, Mdesc) <> Cells(j, Cdesc) And Cells(i, Mfile) <> Cells(j, Cfile) And Cells(i, Mfield) <> Cells(j, Cfield) Then
j = j + 1
ElseIf j = CvarNoLastRow Then
Cells(i, XvarNo).Value = "No"
End If
Next j
Next i
End Sub
the reason for this macro is to compare information between sheet 1 and sheet2.
I 'think' from my code it is actually just comparing the data on sheet 1 with sheet1.
Does this work?
Here my sheet is called "Ad revenues"
Sub test()
Dim lC As Long
Dim strColNames(1 To 4) As String
For lC = 1 To 4
strColNames(lC) = Sheet1.Name & vbTab & Str(Cells(1, lC).Column)
Next lC End Sub
End Sub()
If you need to get the sheet name, all of the following will work:
=SUBSTITUTE(CELL("filename"),LEFT(CELL("filename"),FIND("]",CELL("filename"))),"")
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,31)
The latter works because excel limits you to 31 characters when naming a sheet
To get the name of a column, you can use something like below:
=SUBSTITUTE(MID(ADDRESS(1,CELL("col")),2,2),"$","")
=MID(SUBSTITUTE(ADDRESS(1,COLUMN(R31)),"$1",""),2,2)
Related
Thank you in advance for looking in to this!
I have an issue with a code. What I am trying to do is for the macro in Excel to compare two columns with numbers in two sheets and then based on conditions that the row will also have a string value copy the number that is missing in to the main sheet (I hope it makes sense).
Sub conditionalCopying()
inactiveRow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
cutRow = Worksheets("Cutting").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To inactiveRow
If Worksheets("Inactive").Cells(i, 3).Value = "North" Then
For x = 2 To cutRow
If Worksheets("Inactive").Cells(i, 1) <> Worksheets("Cutting").Cells(x, 1).Value Then
Worksheets("Inactive").Rows(i).Copy
Worksheets("Cutting").Activate
b = Worksheets("Cutting").Cells(cutRow + 1, 1).Select
ActiveSheet.Paste
Worksheets("Inactive").Activate
End If
Next i
Next x
End Sub
Can't include screenshots due to reputation not being high enough, sorry.
Thank you once again for your help.
Maybe give this a try :
Sub conditionalCopying()
Dim Cpt As Long
inactiveRow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
Cutrow = Worksheets("Cutting").Cells(Rows.Count, 1).End(xlUp).Row
Cpt = Cutrow
For i = 2 To inactiveRow
If Worksheets("Inactive").Cells(i, 3).Value = "North" Then
For x = 2 To Cutrow
If Worksheets("Inactive").Cells(i, 1) <> Worksheets("Cutting").Cells(x, 1).Value Then
Cpt = Cpt + 1
Worksheets("Inactive").Rows(i).Copy
Worksheets("Cutting").Activate
b = Worksheets("Cutting").Cells(Cpt, 1).Select
ActiveSheet.Paste
Worksheets("Inactive").Activate
GoTo nexti
End If
Next x
End If
nexti:
Next i
End Sub
Edited Code Give this a try
Sub conditionalCopying()
Dim Cpt As Long
inactiveRow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
Cutrow = Worksheets("Cutting").Cells(Rows.Count, 1).End(xlUp).Row
Cpt = Cutrow
For i = 2 To inactiveRow
If Worksheets("Inactive").Cells(i, 3).Value = "North" And Worksheets("Inactive").Cells(i, 1).Value = "11" Then
Cpt = Cpt + 1
Worksheets("Inactive").Rows(i).Copy
Worksheets("Cutting").Activate
b = Worksheets("Cutting").Cells(Cpt, 1).Select
ActiveSheet.Paste
Worksheets("Inactive").Activate
End If
Next i
ThisWorkbook.Sheets("cutting").Range("A1:D" & Cpt).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlYes
End Sub
Second Edit
Sub conditionalCopying()
Dim Cpt As Long
inactiveRow = Worksheets("Inactive").Cells(Rows.Count, 1).End(xlUp).Row
Cutrow = Worksheets("Cutting").Cells(Rows.Count, 1).End(xlUp).Row
Cpt = Cutrow
For i = 2 To inactiveRow
If Worksheets("Inactive").Cells(i, 3).Value = "North" Then
Cpt = Cpt + 1
Worksheets("Inactive").Rows(i).Copy
Worksheets("Cutting").Activate
b = Worksheets("Cutting").Cells(Cpt, 1).Select
ActiveSheet.Paste
Worksheets("Inactive").Activate
End If
Next i
ThisWorkbook.Sheets("cutting").Range("A1:D" & Cpt).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlYes
End Sub
Image1
Hi, Referring to the image, I am trying to compare column G and Column K, if the value is the same then copy the value in column J to column F. However, my code doesn't copy the value from Column J to F.
Sub createarray1()
Dim i As Integer
Dim j As Integer
Dim masterarray As Range
Set masterarray = Range("D3:G12")
Dim sourcearray As Range
Set sourcearray = Range("H3:K26")
For i = 1 To 10
For j = 1 To 25
If masterarray(i, 4).Value = sourcearray(j, 4).Value Then
masterarray(i, 3) = sourcearray(j, 3).Value
Else
masterarray(i, 3).Value = ""
End If
Next
Next
End Sub
Function concatenate()
Dim nlastrow As Long
For i = 2 To Cells(Rows.Count, "D").End(xlUp).Row
Cells(i, "G").Value = Cells(i, "D").Value & "_" & Cells(i, "E").Value
Next i
Dim nnlastrow As Long
For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
Cells(i, "K").Value = Cells(i, "H").Value & "_" & Cells(i, "I").Value
Next i
End Function
Use variant arrays, that way you limit the number of calls to the sheet to only 3.
When your positive is found you need to exit the inner loop.
Sub createarray1()
Dim i As Long
Dim j As Long
Dim masterarray As Variant
Dim sourcearray As Variant
With ThisWorkbook.Worksheets("Sheet1") ' change to your sheet
masterarray = .Range("D3:G12")
sourcearray = .Range("H3:K26")
For i = LBound(masterarray, 1) To UBound(masterarray, 1)
masterarray(i, 3) = ""
For j = LBound(sourcearray, 1) To UBound(sourcearray, 1)
If masterarray(i, 4) = sourcearray(j, 4) Then
masterarray(i, 3) = sourcearray(j, 3)
Exit For
End If
Next j
Next i
.Range("D3:G12") = masterarray
End With
End Sub
But this can all be done with the following formula:
=INDEX(J:J,MATCH(G3,K:K,0))
Put it in F3 and copy/drag down.
I want vba code to look for specific ranges of data if these data exist in main sheet then retrieve last row of data that mean based on conditions. For example(there are 3 row with "dler" I want to compare dler with three rows of second sheet if all exist retrieve the row of dler) that mean compare name with other rows and so on... The picture is two sheets the first one is (main sheet) and the second one is the table that the vba work on it to find data in (main sheet) I have this code but I don't know how change it to work with dynamic records.
Main and Search Worksheet Image
Sub Matching_name()
Dim a_name As String, i As Long, j As Long, Last_Row As Long
For i = Last_Row To 2 Step -1
a_name = Cells(i, "B").Value
If City = "dler" Then
'Set the range destination, Range(“A2”), depending on which
'range you want in Sheets(“Remaining”)
Rows(i).EntireRow.Copy Destination:=Worksheets("Remaining").Range("A1")
Exit For
End If
Next i
End Sub
This will copy the last matching rows
Sub Matching_name()
Dim i As Long, j As Long, k As Long, Last_Row As Long, temp As Long
Dim a_name As String, s_type As String, c_type As String
temp = 1
Last_Row = 6
For i = 2 To Last_Row
Worksheets("Main Sheet").Activate
a_name = Cells(i, 2).Value
s_type = Cells(i, 5).Value
c_type = Cells(i, 6).Value
Worksheets("Search Sheet").Activate
For j = 1 To 3
If Cells(j, 1).Value = a_name And Cells(j, 2).Value = s_type And Cells(j, 3).Value = c_type Then
Worksheets("Main Sheet").Activate
Rows(i & ":" & i).Select
Selection.Copy
Worksheets("Remaining").Activate
Rows(temp & ":" & temp).Select
ActiveSheet.Paste
temp = temp + 1
End If
Next j
Next i
Worksheets("Remaining").Activate
For x = temp To 1 Step -1
y = 1
While y <= temp
If Cells(y, 2).Value = Cells(x, 2).Value And x <> y Then
Rows(y & ":" & y).Delete
y = y - 1
temp = temp - 1
End If
y = y + 1
Wend
Next x
End Sub
I am adapting off of the question: Re-Arranging the row data in columns
I have Excel data set up as follows;
Collection LatDD LonDD Date Location Method Specie1 Specie2 Specie3(+-110 species columns in total)
ABS1 11.35 -10.3 2003-02-01 A Bucket 0 1 3
ABS2 11.36 -10.4 2003-02-02 B Stick 2 0 6
I would Like This Data to appear like so:
Collection Specie Count LatDD LonDD Date Location Method
ABS1 Specie1 11.35 -10.3 2003-02-01 A Bucket
ABS1 Specie2 1 11.35 -10.3 2003-02-01 A Bucket
ABS1 Specie3 3 11.35 -10.3 2003-02-01 A Bucket
ABS2 Specie1 2 11.36 -10.4 2003-02-02 B Stick
ABS2 Specie2 -11.36 -10.4 2003-02-02 B Stick
ABS2 Specie3 6 -11.36 -10.4 2003-02-02 B Stick
I attempted to adapt Ripsters original VBA code answer but unfortunately i was unable to figure how i would need to change it. Could someone please advise me on how to adjust his code to produce the desired output?
Here is his orginal vba code:
Sub Example()
Dim Resources() As String
Dim rng As Range
Dim row As Long
Dim col As Long
Dim x As Long
ReDim Resources(1 To (ActiveSheet.UsedRange.Rows.Count - 1) * (ActiveSheet.UsedRange.Columns.Count - 1), 1 To 3)
'Change this to the source sheet
Sheets("Sheet1").Select
'Read data into an array
For row = 2 To ActiveSheet.UsedRange.Rows.Count
For col = 2 To ActiveSheet.UsedRange.Columns.Count
x = x + 1
Resources(x, 1) = Cells(row, 1).Value ' Get name
Resources(x, 2) = Cells(1, col).Value ' Get date
Resources(x, 3) = Cells(row, col).Value ' Get value
Next
Next
'Change this to the destination sheet
Sheets("Sheet2").Select
'Write data to sheet
Range(Cells(1, 1), Cells(UBound(Resources), UBound(Resources, 2))).Value = Resources
'Insert column headers
Rows(1).Insert
Range("A1:C1").Value = Array("Resource", "Date", "Value")
'Set strings to values
Set rng = Range(Cells(1, 3), Cells(ActiveSheet.UsedRange.Rows.Count, 3))
rng.Value = rng.Value
End Sub
Try this:
Sub Example()
Dim row As Long
Dim col As Long
Dim x As Long
h1 = "Sheet1"
h2 = "Sheet2"
Sheets(h1).Select
x = 2
'Headers Sheet2
Sheets(h2).Cells(1, 1).Value = Sheets(h1).Cells(1, 1)
Sheets(h2).Cells(1, 2).Value = "Specie"
Sheets(h2).Cells(1, 3).Value = "Count"
Sheets(h2).Cells(1, 4).Value = Sheets(h1).Cells(1, 2)
Sheets(h2).Cells(1, 5).Value = Sheets(h1).Cells(1, 3)
Sheets(h2).Cells(1, 6).Value = Sheets(h1).Cells(1, 4)
Sheets(h2).Cells(1, 7).Value = Sheets(h1).Cells(1, 5)
Sheets(h2).Cells(1, 8).Value = Sheets(h1).Cells(1, 6)
For row = 2 To ActiveSheet.UsedRange.Rows.Count
For col = 7 To ActiveSheet.UsedRange.Columns.Count
Sheets(h2).Cells(x, 1).Value = Sheets(h1).Cells(row, 1).Value
Sheets(h2).Cells(x, 2).Value = Sheets(h1).Cells(1, col).Value
Sheets(h2).Cells(x, 3).Value = Sheets(h1).Cells(row, col).Value
Sheets(h2).Cells(x, 4).Value = Sheets(h1).Cells(row, 2).Value
Sheets(h2).Cells(x, 5).Value = Sheets(h1).Cells(row, 3).Value
Sheets(h2).Cells(x, 6).Value = Sheets(h1).Cells(row, 4).Value
Sheets(h2).Cells(x, 7).Value = Sheets(h1).Cells(row, 5).Value
Sheets(h2).Cells(x, 8).Value = Sheets(h1).Cells(row, 6).Value
x = x + 1
Next
Next
End Sub
Sheet1:
Sheet2:
A short versión:
Sub Example()
Dim row As Long
Dim col As Long
Dim x As Long
Set Sh1 = ThisWorkbook.Worksheets("Sheet1")
Set Sh2 = ThisWorkbook.Worksheets("Sheet2")
Sh1.Select
'Headers Sheet2
Sh2.Cells(1, 1).Value = Sh1.Cells(1, 1)
Sh2.Cells(1, 2).Value = "Specie"
Sh2.Cells(1, 3).Value = "Count"
For i = 4 To 8
Sh2.Cells(1, i).Value = Sh1.Cells(1, i - 2)
Next
x = 2 'Starting row of sheet2.
For row = 2 To ActiveSheet.UsedRange.Rows.Count
For col = 7 To ActiveSheet.UsedRange.Columns.Count
Sh2.Cells(x, 1).Value = Sh1.Cells(row, 1).Value
Sh2.Cells(x, 2).Value = Sh1.Cells(1, col).Value
Sh2.Cells(x, 3).Value = Sh1.Cells(row, col).Value
For i = 4 To 8
Sh2.Cells(x, i).Value = Sh1.Cells(row, i - 2).Value
Next
x = x + 1
Next
Next
Sh2.Select
End Sub
another idea....
Your source data is in "Sheet1", starting at "A1", no empty values neither in Column A, nor in Row 1.
If you run the code, you will get the re-sorted table in "Sheet2" ( I omitted headers, though - too lazy....)
Hope this helps
Sub sort_new()
Dim col_no As Long, row_no As Long
Dim i As Long, j As Long, k As Long
Dim arr_DB As Variant, arr_new As Variant
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
ws1.Activate
row_no = ws1.Range(Cells(Rows.Count, 1), Cells(Rows.Count, 1)).End(xlUp).Row
col_no = ws1.Range(Cells(1, Columns.Count), Cells(1, Columns.Count)).End(xlToLeft).Column
arr_DB = ws1.Range(Cells(1, 1), Cells(row_no, col_no))
ReDim arr_new(1 To (row_no - 1) * (col_no - 6), 1 To 8)
For i = 2 To row_no
For j = 7 To col_no
k = k + 1
arr_new(k, 1) = arr_DB(i, 1) 'Collection
arr_new(k, 4) = arr_DB(i, 2) 'LatDD
arr_new(k, 5) = arr_DB(i, 3) 'LonDD
arr_new(k, 6) = arr_DB(i, 4) 'Date
arr_new(k, 7) = arr_DB(i, 5) 'Location
arr_new(k, 8) = arr_DB(i, 6) 'Method
arr_new(k, 2) = arr_DB(1, j) 'Each Specie(j) Column
arr_new(k, 3) = arr_DB(i, j) 'Each Specie(j) Column
Next
Next
ws2.Activate
ws2.Range(Cells(2, 1), Cells((row_no - 1) * (col_no - 6) + 1, 8)) = arr_new
End Sub
So I have 4 sheets that are called "old", "current", "input", and "buttons". The process is to: press a button on the "buttons" sheet to clear the "current" sheet and "input" sheet, paste data onto the "input" sheet and press a macro button on the "buttons" sheet to populate the "current" sheet. Most of the macro will be formatting the "current" sheet and using index match for data from the "old" sheet. What I'm trying to do is add a step in the beginning to clear the "old" sheet and then copy and paste the data from the "current" sheet onto the "old" sheet. The reason is that I will be using this weekly and every time I run the macro, I want the "current" sheet, that was created last time I ran the macro, to move to the "old" sheet. This is currently the code that I have...
Sub Load16()
Application.ScreenUpdating = False
'Define Workbooks
Dim loopCount As Integer
Dim loopEnd As Integer
Dim writeCol As Integer
Dim matchRow As Integer
Dim writeRow As Integer
Dim writeEnd As Integer
loopEnd = WorksheetFunction.CountA(Worksheets("Input").Range("A:A"))
writeEnd = WorksheetFunction.CountIf(Worksheets("Input").Range("L:L"), "-1")
loopCount = 1
writeRow = 1
Worksheets("Buttons").Range("F17:I17").Copy
Worksheets("Current").Range("J2:M" & writeEnd).PasteSpecial Paste:=xlPasteAll
Application.CutCopyMode = False
Do While loopCount <= loopEnd
If Worksheets("Input").Cells(loopCount, 12).Value <> "" And
Worksheets("Input").Cells(loopCount, 12).Value <> 0 Then
Worksheets("Current").Cells(writeRow, 1).Value = Worksheets("Input").Cells(loopCount, 26).Value
writeCol = 2
Do While writeCol <= 9
Worksheets("Current").Cells(writeRow, writeCol).Value = Worksheets("Input").Cells(loopCount, writeCol - 1)
writeCol = writeCol + 1
Loop
writeCol = 14
Do While writeCol <= 30
Worksheets("Current").Cells(writeRow, writeCol).Value = Worksheets("Input").Cells(loopCount, writeCol - 5)
writeCol = writeCol + 1
Loop
Worksheets("Current").Cells(writeRow, 31).Value = Worksheets("Input").Cells(loopCount, 27)
writeRow = writeRow + 1
Else
End If
loopCount = loopCount + 1
Loop
Worksheets("Current").Range("J1").Value = "Counsel"
Worksheets("Current").Range("K1").Value = "Background"
Worksheets("Current").Range("L1").Value = "Comments"
Worksheets("Current").Range("M1").Value = "BM Action"
Lookup Data for K - M and a few other things
loopCount = 2
Do While loopCount <= loopEnd
matchRow = 0
On Error Resume Next
matchRow = WorksheetFunction.Match(Worksheets("Current").Cells(loopCount, 1).Value, _
Worksheets("Old").Range("A:A"), 0)
If matchRow = 0 Then
Else
Worksheets("Current").Cells(loopCount, 11).Value = Worksheets("Old").Cells(matchRow, 11).Value
Worksheets("Current").Cells(loopCount, 12).Value = Worksheets("Old").Cells(matchRow, 12).Value
Worksheets("Current").Cells(loopCount, 13).Value = Worksheets("Old").Cells(matchRow, 13).Value
End If
Worksheets("Current").Cells(loopCount, 10).Value =
Worksheets("Current").Cells(loopCount, 18).Value
loopCount = loopCount + 1
Loop
Sheets("Current").Range("A2:AE" & loopEnd).Sort Key1:=Sheets("Current").Range("H2"), _
Order1:=xlAscending, Header:=xlNo
Worksheets("Current").Columns("A:BZ").AutoFit
Application.ScreenUpdating = True
Worksheets("Buttons").Select
MsgBox loopEnd - 1 & " Rows processed. " & writeEnd & " Rows remain."
End Sub
Thanks guys.
A small function like this should do the trick.
Sub copy_current_data()
'Select Old Sheet
Sheets("Old").Select
'Clear all cells from Old Sheet
Sheets("Old").Cells.ClearContents
'Copy Cells from Current Sheet
Sheets("Current").Cells.Copy
'Select "A1" in old sheet
Sheets("Old").Range("A1").Select
'Paste Data
ActiveSheet.Paste
End Sub