excel macro to add value from cell to a table - vba

I am looking to use a macro to add the value of a certain cell in a worksheet to a table in another worksheet each time each time a button is pressed. For example, the first time the button is pressed I want the value of cell A1 in worksheet2 to be equal to cell C3 in worksheet1, next time B1 in worksheet2 is equal to C3 in worksheet1 , and so on. The macro should only add a value to the table if the cell thst it's being added to is empty.
This is what I have so far:
Sub Button32_ClickandUpdate
myVariable = Worksheets("Worksheet1").Range("C3")
For i = 1 To 6
If IsEmpty(Cells(1, i)) Then
Worksheets("Worksheet2").Range(Cells(1,i)) = myVariable
Exit Sub
End If
Next i
End Sub
Any help would be greatly appreciated.

Try the code below, to update one cell at a time (when cell is empty)
Sub Button32_ClickandUpdate()
For i = 1 To 6
If IsEmpty(Sheets("Worksheet2").Cells(1, i).Value) Then
Sheets("Worksheet2").Cells(1, i).Value = Sheets("Worksheet1").Range("C3").Value
Exit Sub
End If
Next i
End Sub
Comment: you can use this also without the .Value

Try this:
Sub Button32_ClickandUpdate
myVariable = Worksheets("Worksheet1").Range("C3")
For i = 1 To 6
If IsEmpty(Worksheets("Worksheet2").Cells(1, i)) Then
Worksheets("Worksheet2").Cells(1, i) = myVariable
'Exit Sub (remove or comment this line to update all cells at the sime time)
End If
Next i
End Sub
Edit
According to new description given:
Sub Button32_ClickandUpdate
myVariable = Worksheets("Worksheet1").Range("C3")
i = 1
While i < 7
If IsEmpty(Worksheets("Worksheet2").Cells(1, i)) Then
Worksheets("Worksheet2").Cells(1, i) = myVariable
End If
i = i + 1
Wend
End Sub

Related

Go to next empty cell in column B

im trying to create a button for my listbox to go to the next empty cell un column B. when i use this code it goes to the last empty cell in column A. please help. thanks
Private Sub CommandButton6_Click()
Dim i As Long
With lstOCC
For i = .ListCount - 1 To 0 Step -1
Debug.Print i, lstOCC.List(i, 0)
If lstOCC.Column(i, 0) <> "" Then
.ListIndex = i
Exit For
End If
Next i
End With
End Sub

How to create a loop inside a loop vba

I am trying to create this loop inside the loop so as to print inside the cells in the below range -> Week i where i is from 1 to 6 and it raises by one each time we move to a cell down...
So, in this case, for D2 I want Week 1, For D3 Week 2 etc.. Any ideas? I appreciate your time!
Sub INPUT()
Sheets("1").Select
For Each cell In range("D2:D7")
For i = 1 To 6
cell.Value = "Week +" & i
i = i + 1
Next i
Next cell
End Sub
You only need the one loop:
Sub INPT()
With Sheets("1")
i = 1
For Each cell In .Range("D2:D7")
cell.Value = "Week +" & i
i = i + 1
Next cell
End With
End Sub

Highlight values on Sheet1 if matched on Sheet2

I'm looking for a way to highlight cells in sheet1 if they match the value in sheet2. Here is the code I have, there aren't any errors coming up but it does nothing. Basically I thought a Do while loop to go through all the records until it hit a blank and then it would read the cell value selected by my offset and compare it to the next sheets cell value while staying on the same row, and if it matched it would highlight on sheet 1 but if it didn't it would move on. Let me know how much I'm off here as I don't have much VBA knowledge. Thanks.
Public Sub RoundedRectangle1_Click()
Dim resource As Range
Dim register As Range
Dim cancel As Range
Set resource = Worksheets("Resource List1").Cells(2, 4)
Set register = Worksheets("Registered List").Cells(2, 1)
Set cancel = Worksheets("Cancelled List").Cells(2, 1)
Call findRegister(resource, register)
End Sub
Public Sub findRegister(ByRef resource As Range, ByRef register As Range)
Dim i As Integer
i = 0
Do While resource.Offset(i, 3) <> ""
If resource.Offset(i, 3).Value = register.Range("A2").Value Then
resource.Offset(i, 3).Cells.Interior.ColorIndex = 37
End If
i = i + 1
Loop
End Sub
Your code is essentially correct, but I think you're having trouble with referencing the right cells. A good debugging technique would be to add .Cells.Interior.ColorIndex = 4 or something similar in your code to see visually whether you're referencing the proper cells. You can also put "F5", "F8", and breakpoints to good use in figuring out what's wrong. See http://www.excel-easy.com/vba/examples/debugging.html if you've never used these.
For example:
Do While resource.Offset(i, 3) <> "" '<--Insert a breakpoint on this line,
'then press "F8" to make sure the
'code inside your Do While loop is
'being executed
resource.Offset(i, 3).Cells.Interior.ColorIndex = 4
register.Range("A2").Cells.Interior.ColorIndex = 6
If resource.Offset(i, 3).Value = register.Range("A2").Value Then
resource.Offset(i, 3).Cells.Interior.ColorIndex = 40
End If
i = i + 1
Loop
Maybe something as simple as this . . . .
Sub Compare2Shts()
For Each Cell In Worksheets("CompareSheet#1").UsedRange
If Cell.Value <> Worksheets("CompareSheet#2").Range(Cell.Address) Then
Cell.Interior.ColorIndex = 3
End If
Next
For Each Cell In Worksheets("CompareSheet#2").UsedRange
If Cell.Value <> Worksheets("CompareSheet#1").Range(Cell.Address) Then
Cell.Interior.ColorIndex = 3
End If
Next
End Sub

How do I make value static in last row value?

Here is the code below:
Public n as Long ' <--above sub procedure
With Sheets("Sheet1").Range("A6").Offset(n, 0)
If n = 0 Then
.Value = 1
Else
.Value = .Parent.Range(.Address).Offset(-1, 0) + 1
End If
n = n + 1
End With
(See pic below) If I delete 4 then click command button again it just reset back to 1. I want to make it static so even I deleted the last value of row it still continue increment from the last value.
Store number
1
2
3
4
Try this:
Sub Test()
Dim trow As Long
With Sheets("Sheet1") '~~> change to suit
trow = .Range("A:A").Find(vbNullString, [A5]).Row
With .Range("A" & trow)
If trow = 6 Then .Value = 1 _
Else .Value = .Offset(-1, 0).Value + 1
End With
End With
End Sub
Above code finds the first blank cells. If it is A6 it assigns a value of 1.
Otherwise it assigns previous cell value plus 1. Is this what you're trying?
Edit1: Explanation
trow = .Range("A:A").Find(vbNullString, [A5]).Row
This finds the first empty row in Column A starting A5.
[A5] is used to return Range("A5") object. So it can also be written as:
trow = .Range("A:A").Find(vbNullString, .Range("A5")).Row
We used a VBA vbNullString constant as What argument in Range Object Find Method.
Find Method returns a Range Object so above can be written also like this:
Sub Test()
Dim r As Range
With Sheets("Sheet1") '~~> change to suit
Set r = .Range("A:A").Find(vbNullString, [A5])
With r
If .Row = 6 Then .Value = 1 _
Else .Value = .Offset(-1, 0).Value + 1
End With
End With
End Sub
What your asking for, a button with memory doesn't sound neatly solvable using just VBA.
You could potentially have a list on a hidden sheet that gets a value added to it each time the commandButton is pressed and it writes the max of the list values back to the target cell?
Alternatively you could investigate using a scrollbar from the form control section of the developer tab with a link to your target cell. I often use this technique for interactive sheets.
Named Range Method
Public sub btnPress
dim val as long
val = Range("PreviousCellValue")
set Range("PreviousCellValue") = val+1
Sheets("Sheet1").Range("A6").Offset(n, 0).value = Range("PreviousCellValue")
End sub btnPress

VBA( macros) copy and paste

I am trying to create a macros that will allow me each time it's activated to copy the value of a cell in worksheet 1 (the same cell but which would probably have differrent results after my calculation) and to paste the value of those results in worksheet 2 (maybe in A1;A2;A3;....... each time I make a calcul) this is a sample of a code i have written but which isn'working:
Sub recorder()
If Cells(B, i) <> Empty Then
i = i + 1
Worksheets(1).Select
Cells(A1).Copy
Worksheets(2).Select
Cells(B, i) = Cells(A1)
End If
End Sub
Any help would be appreciated. Thanks
Paste this into ThisWorkbook.
Whatever you change the value in Cell A1 on Sheet1 it will appear on Sheet2 in the nearest blank cell in column A. Note you don't need to run a macro it happens automatically.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Sh.Index = 1 Then Exit Sub
If Not Target.Address = "$A$1" Then Exit Sub
If Worksheets(2).Range("A65536").End(xlUp).Value = Empty Then
Worksheets(2).Range("A65536").End(xlUp).Value = Target.Value
Else
Worksheets(2).Range("A65536").End(xlUp).Offset(1, 0).Value = Target.Value
End If
End Sub
I think this is what you're looking for:
Sub recorder()
Sheets(2).Cells(Rows.Count, "B").End(xlUp).Offset(1).Value = Sheets(1).Range("A1").Value
End Sub