Excel VBA 2 Sheets compare and fill if empty - excel

I got 2 Sheets each with a table.
The tables on those sheets have the same format, same length etc.
Row 1 is the days of the month and column A are the employees.
Now I want to compare those two sheets. Sheet1 is the main, if one cell is empty I would like to check if there is data in Sheet2 same cell. If yes, copy it into Sheet1. If no, leave empty.
Is this possible?
VLOOKUP semms like the simple solution but I cant figure out how to do it with 2 criterias (Name and Date)
For filling empty cell i got this code but dont know if thats the best method.
Sub Fill_empty_cell()
Set MR = Range("C3:X600")
For Each cell In MR
If IsEmpty(cell.Value) = True Then
cell.Value = "VLOOKUP????"
End If
Next
End Sub
Thanks for any help!
Daniel

Try this:
Sub Fill_empty_cell()
Dim sheet1 As Worksheet
Dim sheet2 As Worksheet
Dim rangeToCheck As Range
Set sheet1 = Sheets("Test1")
Set sheet2 = Sheets("Test2")
Set rangeToCheck = sheet1.Range("A2:D30")
For Each cellToCheck In rangeToCheck
If IsEmpty(cellToCheck.Value) = True Then
cellToCheck.Value = sheet2.Cells(cellToCheck.Row, cellToCheck.Column).Value
End If
Next
End Sub
Here you can set a Range by String...

Yes this is possible the easiest way would be using For loops and defining all cells in the range by number (cells(x,y)) and use an if statements to compare those for the different sheets (eg. sheet1.cells(x,y) to sheet2.cells.(x,y))
The if checks if its empty if sheet2 is empty if will stay empty but if somethings is their it will be put in sheet 1
Sub Fill_empty_cell()
Dim x As Long
Dim y As Long
x = 3
y = 3
For x = 3 To 21 'this is the column number of x I think please check
For y = 3 To 300
If ThisWorkbook.Sheets(1).Cells(y, x) = "" Then
ThisWorkbook.Sheets(1).Cells(y, x) = ThisWorkbook.Sheets(2).Cells(y, x)
End If
Next y
Next x
End Sub

Related

Sum with dynamic range

I am trying to sum the cells from T to X, and insert the result into cell Y. I'm starting at row 50, and I'd like to end at the last possible row in the sheet so that it works dynamically. Unfortunately, by designating the last row in the table the macro stops working. I've read a lot of tutorials, I've even copied the same macros that people have created and it still doesn't work.
Subb Zad()
Dim x As Long
For x = 50 To Cells(Rows.Count, "Y").End(xlUp).Row
Cells(x, "Y").Formula = Replace("=sum(T#:X#)", "#", x)
Next
End Sub
No Need for the loop just apply the relative formula to all rows at once:
Sub Zad()
With ActiveSheet 'good practice to denote the parent sheet even if it is the active sheet
Dim x As Long
x = .Cells(.Rows.Count, "T").End(xlUp).Row 'Use T as Y may be empty
.Range(.Cells(50,"Y"),.Cells(x,"Y")).Formula = "=SUM(T50:X50)"
End With
End Sub

Hide multiple row if their requirement is met

I am new to coding in anything, this project is the first time I have coded. I am trying to hide multiple row based on individual requirement. The requirement is if in a specific cell of the same row there is a space or is empty, the row will be hidden, if it is hidden and there is anything else, the row will be shown. The code need to work on specific worksheet as I have multiple worksheet where there is row to hide or columns to hide at different place.
There are 2 different pieces of code that I tried which don't work.
This picture represent the Excel sheet I am currently trying to hide row:
My goal is to hide row between 8 to 37 if there is there is a space or if it is empty, depending what the code inside the cell point at for the cell A8 to A37. if I activate the code, in the image only the row 8, 9 and 10 should be visible, 11 to 37 should be hidden.
So far I have tried these two pieces of code:
Sub C1()
Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Dim C As range
For Each C In range("A8:A37")
If C.Value = " " Then
C.EntireRow.Hidden = True
Else
If C.Value = Empty Then
C.EntireRow.Hidden = True
Else
C.EntireRow.Hidden = False
End If
End If
Next C
End Sub
This code work as intended except that it is not tied to a sheet. "Set ws = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")" is not working as well as a couple other code I tried, they point to an error. So when I try to use this code it will work on the active sheet and not "FR-3-06_Jeux Prod."
Sub Hide_column_and_Row_F_3_6()
Dim NbreLigne As Integer
Dim tableau As range
Set wrkshtDoc = ActiveWorkbook.Worksheets("FR-3-06_Jeux Prod.")
Set tableau = wrkshtDoc.range("A8:A37")
NbreLigne = tableau.Rows.Count
For k = 1 To NbreLigne
If tableau(1, k) = " " Then
tableau(1, k).EntireRow.Hidden = True
ElseIf tableau(1, k) = Empty Then
tableau(1, k).EntireRow.Hidden = True
Else
tableau(1, k).EntireRow.Hidden = False
End If
Next k
End Sub
This code only works as intended when I try to hide columns as in replace "row" in the code with "columns". There is sheet in my file where is it columns I need to hide and since this code is working I tried to reuse it... what it is currently doing is hiding row with "test", line 8 only. It wont hide the empty cell.
what would be the error or what would be needed to hide row with the requirement? I know that code #2 work with columns...
You are almost there with code1, you only need to add:
For each C in ws.Range("A8:A38")
Because you add ws. in front of the Range, it knows which sheet to apply it on.
Good luck!
Hide Blank Rows
Option Explicit
Sub HideBlankRows()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("FR-3-06_Jeux Prod.")
Dim Cell As Range
For Each Cell In ws.Range("A8:A37").Cells
Cell.EntireRow.Hidden _
= IIf(Len(Trim(CStr(Cell.Value))) = 0, True, False)
Next Cell
End Sub

Order sheets based on input in Excel spreadsheet

In the Excel spreadsheet the user can define the order of the sheets within Column A:
A B
1 Sheet3
2 Sheet4
3 Sheet1
4 Sheet2
5
Once the user enterred the order in Column A he/she can click a button which is linked to this VBA code:
Sub Move()
Sheets(Sheet1.Range("A2").Value).Move After:=Sheets(Sheet1.Range("A1").Value)
Sheets(Sheet1.Range("A3").Value).Move After:=Sheets(Sheet1.Range("A2").Value)
Sheets(Sheet1.Range("A4").Value).Move After:=Sheets(Sheet1.Range("A3").Value)
End Sub
This VBA puts the sheets in the order based on the inputs from the user in Column A. All this works fine so far
Now I have the issue that the number of sheets varies so it can happen that instead of only 4 sheets there will be 8 or 10 or 15 and so on. In this case it would be necessary to add all those sheets manually to the VBA code.
Is it possible to make the VBA code more dynamically. Something like an array for the values in Column A and a VBA like this:
Sub Move()
MoveSheets based on Array {Sheet1.Range("A1:A5")}
End Sub
I think this will work if your sheet names are on "Sheet1" in B4 going down.
Sub x()
Dim n As Long, i As Long, r As Range
Application.ScreenUpdating = False
With Sheets("Sheet1")
Set r = .Range("B4", .Range("B" & Rows.Count).End(xlUp))
For n = r.Count To 1 Step -1
Sheets(r.Cells(n).Value).Move after:=Sheets(Sheets.Count - i)
i = i + 1
Next n
Application.Goto .Range("A1")
End With
Application.ScreenUpdating = True
End Sub

Excel VBA code to copy a row if an uppercase or a lowercase in a column to next sheet

I want to copy a row to another sheet (sheet4) when in column Z a upper case X is present and when on another row a lower case x is present in the same column. Thus automatically copying both rows to sheet4.
To further explain
I need the VBA code to pick up both letters throughout the current sheet in just column Z
Thanks
Something like this shoudl do it.
Sub CopyData()
Dim Rng As Range, cell As Range
Dim rw As Long
Set Rng = Worksheets("Sheet1").Range("Z:Z")
rw = 1
For Each cell In Rng
If LCase(cell.Value) = "X" Then
Worksheets("Sheet2").Cells(rw, "A") = cell.Offset(0, -1)
rw = rw + 1
End If
Next
End Sub
However, I don't know what this means:
"when on another row a lower case x is present in the same column."
What is the logic???

Write to two cells at same time excel vba

I need to write the same data into two different range of cells for a VBA application that I am writing. I could of course just loop through twice and write the data, but I was hoping to do it in one pass.
This is an example of what I am doing (lot of complexity removed).
Sub WriteData()
WriteOutDivision "Division1",10
WriteOutDivision "Division1",20
End Sub
Private Sub WriteOutDivision(ByVal divisionName, ByVal rowNumber)
Dim curSheet As Worksheet
Set curSheet = Sheets("Company Scorecard")
With curSheet.Cells(rowNumber, 1)
.value = divisionName
.Font.Bold = True
.InsertIndent 1
End With
End Sub
Is there something that I can do to write to both row 10 column 1 and row 20 column 1 at the same time?
You could define a non-contigous range and change the propeties of the range. For example:
Dim curSheet As Worksheet
Dim rngSpecial As Range
Set curSheet = Sheets("Company Scorecard")
Set rngSpecial = curSheet.Range("A1,A3")
rngSpecial.Value = "Whatever"
Will write "Whatever" in A1 and A3.
Could you just pass an array of row numbers instead of one rowNumber, then in your function just loop through each element in the array (I don't remember, I haven't programed in VBA for a while)? Then you could print it to as many rows as you want.

Resources