Excel VBA - Merge across - vba

I have a tricky situation. I have a column A with only headers and column B contains text. Now I would like to get the text from column B to start in column A. If there's text in column A, B will always be empty.
A B
Title 1
Text 1
Text 2
Title 2
Text 1
Text 2
How could I get it so the text in column B is put in column A. Range is set until a complete empty row is found. (A1 to S1 no values in the cells = empty row)
I was thinking about merging cells, but that's perhaps not neat.

Like this? This uses merging and also takes into account where A and B are both filled up.
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Sheets("Sheet5")
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Application.WorksheetFunction.CountA(.Range("A" & i & ":" & "B" & i)) = 1 And _
Len(Trim(.Range("A" & i).Value)) = 0 Then
With .Range("A" & i & ":" & "B" & i)
.Merge
End With
End If
Next i
End With
End Sub
FOLLOW UP
If you don't want merging and A will always remain empty when there is a value in B then we can move the value from Col B into Col A like this
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Sheets("Sheet5")
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Len(Trim(.Range("A" & i).Value)) = 0 Then
.Range("A" & i).Value = .Range("B" & i).Value
.Range("B" & i).ClearContents
End If
Next i
End With
End Sub

Related

VBA to populate names

I need help to create one VBA for below issue -
column C should be updated based on column A and B values.
in below example if column A=Unit1 and column B=IND then Person 1 and so on.. it should loop till end.
Thank you in adv. please refer picture for sample data
enter image description here
Here you go. Just change the sheet name since I don't know what you're working with.
Sub AddPerson()
Dim i As Long, ws As Worksheet, lRow As Long
Set ws = Sheets("Sheet1") 'Change to your sheet name
lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
With ws
.Range("C2:C" & lRow + 1).ClearContents
For i = 2 To lRow
If .Range("B" & i) = "IND" Then
.Range("C" & i) = "Person1"
ElseIf .Range("A" & i) = "Unit5" And .Range("B" & i) = "OTR" Then
.Range("C" & i) = "Person3"
Else
.Range("C" & i) = "Person2"
End If
Next i
End With
End Sub

How to start capturing data from a specific row(say A8) value using Userform

Desired Output should start from A8 rowI want to start capturing the data from a particular row range say A8 and continuing to next rows. In my code I am getting an error in selecting the range can anyone guide me to solve this error.
Below is the attached code:
Private Sub cmdadd_Click()
Dim StockNumber As Long, LastRow As Long
Dim wsStockSheet As Worksheet
Set wsStockSheet = ThisWorkbook.Worksheets("Sheet1")
StockNumber = Application.Max(wsStockSheet.Columns(1)) + 1
With wsStockSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Range("A8").Select
.Range("A" & LastRow).Value = StockNumber
.Range("B" & LastRow).Value = Me.cbodd.Text
.Range("C" & LastRow).Value = Me.txtPro.Text
.Range("D" & LastRow).Value = Me.txtEn.Text
End With
End Sub
Your own code commented.
Private Sub cmdadd_Click() 'each time you press button cmadd
Dim StockNumber As Long, LastRow As Long
Dim wsStockSheet As Worksheet
Set wsStockSheet = ThisWorkbook.Worksheets("Sheet1")
StockNumber = Application.Max(wsStockSheet.Columns(1)) + 1 'Find the maximun value in column A. Then add one.
With wsStockSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 'Find last row. Then add one (i.e., the row following last with data)
If (LastRow < 7) Then LastRow = 8 'If last row with data is less than 8, then make it be 8
'in columns A, B, C and D of that row, write the corresponding textbox data
.Range("A" & LastRow).Value = StockNumber
.Range("B" & LastRow).Value = Me.cbodd.Text
.Range("C" & LastRow).Value = Me.txtPro.Text
.Range("D" & LastRow).Value = Me.txtEn.Text
End With
End Sub

Match two sheets to find differences

I have one document (two sheets) that I am trying to compare between. I have cleaned up the columns so both have our unique reference number in column A, the vendor expense in column B and the revenue in column C. I am trying to do an internal audit of sorts without going through every one individually.
One sheet contains data from two years whereas the other contains data from one year. It is not a definitive date so I didn't want to remove any.
Accountants Export
My Data
How would I go about matching the unique identifier in column A and highlighting if there is a difference in the information in column B or C?
here is the code to do it in VBA
I suppose for both Sheet1 and Sheet2 that:
ColumnA is "Pro", ColumnB Gross Rate and ColumnC "Carrier Exp"
All headers are in Row1 and the data starts in row2
Here is the code:
Sub test()
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lastrow As Long, Lastrow2 As Long
Dim i As Integer, j As Integer
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
Lastrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
Lastrow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
For j = 2 To Lastrow2
If ws1.Range("A" & i).Value = ws2.Range("A" & j).Value Then
If Not ws1.Range("B" & i).Value = ws2.Range("B" & j).Value Then
ws1.Range("B" & i).Interior.Color = vbYellow
ws2.Range("B" & j).Interior.Color = vbYellow
End If
If Not ws1.Range("C" & i).Value = ws2.Range("C" & j).Value Then
ws1.Range("C" & i).Interior.Color = vbYellow
ws2.Range("C" & j).Interior.Color = vbYellow
End If
End If
Next j
Next i
End Sub

How to sum values in variable range in VBA?

I have a table as shown below.
In column C I would like to Sum values from column A if they have the same index (column B). I would like to put sum result for all the rows if they have same index (as shown in column D).
Unfortunately the range of values with same index is variable and my macro can sum values just with 2 indexes. Can anyone help with it, please? Thanks!
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Dim n, i As Integer
Set ws = ActiveWorkbook.Sheets("Sheet2")
ws.Select
LastRow = Sheets("Sheet2").Range("A" & Sheets("Sheet2").Rows.Count).End(xlUp).Row
Range("C3:C" & LastRow).Select
Selection.ClearContents
For i = 3 To LastRow
If Range("B" & i + 1) - Range("B" & i) = 0 Then
Range("C" & i) = Range("A" & i + 1) + Range("A" & i)
Else
Range("C" & i) = Range("C" & i - 1)
End If
Next i
End Sub
Here's one way:
Sub example()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = ActiveWorkbook.Sheets("Sheet2")
LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
With ws.Range("C3:C" & LastRow)
.ClearContents
.Value = ws.Evaluate("INDEX(SUMIF(B3:B" & LastRow & ",B3:B" & LastRow & ",A3:A" & LastRow & "),)")
End With
End Sub

Compare between sheets in excel vba

I'm new to VBA and currently busting my head to maintain item list in excel which is connected to database.
sheet1 columns (starts from column B)
UID | itemno. | itemweight | processed
sheet2 same headers(starts from column A)
UID | itemno. | itemweight | deleted
I framed this theoretical target and coded it, tried several times.
It is not looping :( and not achieving my target.
Any suggestions will be helpful! Thanks in advance
Following are my steps framed with the code:
Private Sub CommandButton3_Click()
' Loop until no new UID found
' 1. GoTo Sheet2 to First/Next Cell with UID
' 2. Read UID value
' 3. GoTo Sheet1
' 4. Search in UID column for read UID
' 5. if UID found
' 5.1 get data from Sheet1
' 5.2 GoTo Sheet 2
' 5.3 Past data in right cells
' 5.4 GoTo Sheet 1
' 5.5 Put check flag in proccessed field
' 6. if UID NOT found
' 6.1 GoTo Sheet 2
' 6.2 Put delete flag in delete field
' Loop End
'
' GoTo Sheet1
' Search for all parts with no checked process flag
' Copy datasets
' GoTo Sheet2
' Add data to the end of table
---------------------------------------------------
Dim dict As Object
Dim proc As Range
Dim del As Range
Dim chk, myrange As Range
Set dict = CreateObject("Scripting.dictionary")
Dim sheet1 As Worksheet, Sheet2 As Worksheet
Set sheet1 = ThisWorkbook.Worksheets("MetadataSheet")
Set Sheet2 = ThisWorkbook.Worksheets("PlanningData")
' Read values from sheet2 to dictionary
Dim lastRow As Long
lastRow = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 1 To lastRow
' Store value to dictionary
dict(Sheet2.Cells(i, 1).Value) = 1
Next
' Read from sheet1 and check if each value exists
lastRow = sheet1.Cells(sheet1.Rows.Count, 2).End(xlUp).Row
For i = 1 To lastRow
' Check if value exists in dictionary
If dict.exists(sheet1.Cells(i, 2).Value) Then
' found
sheet1.Range("B2:D2").Select
Selection.Copy
Sheet2.Select
Sheet2.Range("A2:C2").Select
ActiveSheet.Paste
sheet1.Select
sheet1.Range("E2").Select
Set proc = sheet1.Range("E2")
proc.Value = "X"
Else
' not found
Sheet2.Select
Set del = Sheet2.Range("D2")
del.Value = "X"
End If
Next
'for initial load
sheet1.Select
Set chk = sheet1.Range("E2", "E" & lastRow)
For Each chk In myrange
If chk.Value = "" Then
chk.Range("B2:D2").Select
Selection.Copy Destination:= _
Sheets(2).Range("A65536").End(xlUp)(2, 1)
End If
Next chk
End Sub
This code is almost working good. For the first run it loads the data from sheet1 to sheet2.
During second run I get an type mismatch error!
Dim x As Long
Dim y As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow1, lastRow2, lastrow3 As Long
Dim proc As Range
Dim del As Range
Set ws1 = Worksheets("Metadatasheet")
Set ws2 = Worksheets("PlanningData")
y = 2 'this is the first row where your data will output
x = 2 'this is the first row where you want to check for data
lastRow1 = ws1.Range("B:B").Find("*", SearchDirection:=xlPrevious).Row
lastRow2 = ws2.Range("A:A").Find("*", SearchDirection:=xlPrevious).Row
If Not IsEmpty(ws2.Range("B2").Value) Then
Do Until ws1.Range("B2") = ""
For x = 2 To lastRow2
If ws1.Range("B2", "B" & lastRow1).Value = ws2.Range("A2", "A" & lastRow2).Value Then
ws2.Range("A" & y).Value = ws1.Range("B" & x).Value
ws2.Range("B" & y).Value = ws1.Range("C" & x).Value
ws2.Range("C" & y).Value = ws1.Range("D" & x).Value
Set proc = ws1.Range("E" & y)
proc.Value = "X"
y = y + 1
Else
Set del = ws2.Range("D" & y)
del.Value = "X"
End If
Next x
Loop
Else
'lastrow3 = ws1.Range("E:E").Find("*", SearchDirection:=xlPrevious).Row
For x = 2 To lastRow1
If Not ws1.Range("E" & y).Value = "X" Then
ws2.Range("A" & y).Value = ws1.Range("B" & x).Value
ws2.Range("B" & y).Value = ws1.Range("C" & x).Value
ws2.Range("C" & y).Value = ws1.Range("D" & x).Value
y = y + 1
End If
Next x
End If
End Sub