Adding data from one column to existing column in excel using Macros - vba

I am new to excel and using macros, how can i achieve below inquiry
How to add data to another column by pressing a button in excel?
When user press "SAVE", data from column B2 will be added to column of column F.
I tried to do below code but i doesn't work
Sub ButtonSave_Click()
Range("B2").Value = Range("F" & Row).Insert
End Sub
How to deal with this?
thanks in advance!

Use the following and replace Activesheet with the actual sheet name e.g. Worksheets("Sheet1")
Code:
Option Explicit
Public Sub ButtonSave_Click()
With ActiveSheet '<== replace with actual sheet reference
.Range("F" & .Cells(.Rows.Count, "F").End(xlUp).Row + 1) = .Range("B2")
End With
End Sub

Row=Range("F1").End(xlDown).Offset(1,0).Row
Range("F" & Row).Value=Range("B2").Value

Related

Excel: How to copy a row if it contains certain text to another worksheet (VBA)

I'm looking to use a marco that would be able to search a column in said sheet and if certain text is found - in my case the word "FAIL" - copy that entire rows data/formatting and paste it into another sheet - sheet 4 in my case - along with any other rows that contained that specific text.
i have been using this code but it only copy pastes one row then stops rather than going through and copying any rows with "FAIL"
Sub Test()
For Each Cell In Sheets(1).Range("H:H")
If Cell.Value = "FAIL" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets(4).Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets(4).Select
End If
Next
End Sub
First post and brand new to VBA so apologies if too vague.
Try the code below (explanation inside the code as comments):
Option Explicit
Sub Test()
Dim Cell As Range
With Sheets(1)
' loop column H untill last cell with value (not entire column)
For Each Cell In .Range("H1:H" & .Cells(.Rows.Count, "H").End(xlUp).Row)
If Cell.Value = "FAIL" Then
' Copy>>Paste in 1-line (no need to use Select)
.Rows(Cell.Row).Copy Destination:=Sheets(4).Rows(Cell.Row)
End If
Next Cell
End With
End Sub
Try like this:
Option Explicit
Sub TestMe()
Dim Cell As Range
Dim matchRow As Long
With Worksheets(1)
For Each Cell In .Range("H:H")
If Cell.Value = "FAIL" Then
matchRow = .Cell.Row
.Rows(matchRow & ":" & matchRow).Select
.Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Worksheets(4).Select
Worksheets(4).Rows(matchRow).Select
Worksheets(4).Paste
.Select
End If
Next
End With
End Sub
The problem in your code is that you do not reference the worksheets all the time correctly. Thus it does not work correctly.
As a 2. step, you can try to avoid all the selections in your code, it is a best practice to avoid using either Select or Activate in Excel VBA.

Make an Excel Macro that asks for a day and then insert it into the next open cell

I am trying to make an Excel Macro that asks for a day and then inserts it into the next open cell. here is the code I am trying to use.
Sub Button7_Click()
Dim AddName As String
AddName = InputBox("Date: Month/Year", "Add Date", "01/00")
Sheets("Sheet2").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range("A1").Value = AddName
End Sub
It is putting the day I type in into cell A1 no matter what is in it, and then selecting the next open cell in row A.
I feel like the answer is so simple but I can't figure it out!
See How to avoid using Select in Excel VBA macros.
Sub Button7_Click()
Dim AddName As String
AddName = InputBox("Date: Month/Year", "Add Date", "01/00")
With Worksheets("Sheet2")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = AddName
End With
End Sub
Your original problem was that after selecting a cell offset down one row from the last used cell in column A, you didn't use that selection to write the value; just wrote it into A1 instead. The last line of your code could be changed to Selection = AddName but it is better to avoid using select and activate whenever possible.
You may want to look into Application.InputBox. The Excel Application.InputBox Method is slightly different than a standard VBA InputBox function in that it allows to specify a type of return.
Here's another:
Sub Button7_Click()
Dim AddName As String
AddName = InputBox("Date: Month/Year", "Add Date", "01/00")
Sheets("Sheet2").Select
Range("A" & ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row + 1) = AddName
End Sub

VBA: insert max formula, referencing column of a closed workbook

First Post. I have a workbook that is a list of customer names, each name is a link to their individual workbook that is on a sharepoint site. The second column is a "Last contact" column which has a formula
=MAX('https://example.com/folder/Folder/Customer Library/Area/T/[Customername.xlsm]Notes'!$B$2:$B$120)
I am able to make this formula manually by opening the linked workbook, selecting the cell on my customer list, enter "=max(" then highlight the range i want on the linked workbook. Works Fine.
Enter VBA because we want to simplify the procedure for adding customers to the list. I have a userform where you type in the customer name in a textbox1, and the address of the sharepoint wb in textbox 2. The submit button has the following code which inserts the customer name with a link to the sharepoint book in target cell of column A. This works fine. I also am trying to insert the MAX formula in the adjacent cell of column B. I can get the formula to the correct place. However, I am running into issues on the web reference, as it is textbox2's value. When I use textbox2.value it gives errors. Sorry for the book, here is the code without the web book reference:
Private Sub CommandButton1_Click()
Sheets("Dashboard").Select
NextFree = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
ActiveCell.Hyperlinks.Add Anchor:=ActiveCell.Offset(0, 0), Address:=TextBox2.Value, TextToDisplay:=TextBox1.Value
Range("B" & NextFree).Formula = "=MAX(Notes'!$B$2:$B$120)"
UserForm1.Hide
Unload Me
End Sub
Between =Max( & Notes'... I know there needs to be textbox2 value, but I don't understand the syntax I suppose. The "Notes" is the sheet where the range is on the sharepoint wb. The range is the same for all additions to the customer list. Thank you for your help.
Update: I am now trying this code but get error Type Mismatch.
Private Sub CommandButton1_Click()
Sheets("Dashboard").Select
Dim SPbook As Workbook
Dim SPsheet As Worksheet
Dim SPrange As Range
Set SPbook = Workbooks.Open(TextBox2.Value)
Set SPsheet = Sheets("Notes")
Set SPrange = SPsheet.Range("B1:B120")
NextFree = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("A" & NextFree).Select
ActiveCell.Hyperlinks.Add Anchor:=ActiveCell.Offset(0, 0), Address:=TextBox2.Value, TextToDisplay:=TextBox1.Value
Range("B" & NextFree).Formula = "=MAX([" & SPrange & "])"
UserForm1.Hide
Unload Me
End Sub
You should reference textbox2.Value as follows:
Range("B" & NextFree).Formula = "=MAX(" & textbox2.Value $ "Notes'!$B$2:$B$120)"
Formula field takes a string so you just have to build the string concatenating your values.
If textbox2.Value is giving you an error this would not resolve this. Please include more details of this error including complete error message and what values are being passed to subroutine from the form. Regards,

macros vba need to apply formula to column till it has value in the last row

I need to apply "IF "formula in whole C column till has last value in the sheet using VBA .
but i am getting error 438 if i use the following code . Plz help me
Sub test11()
With Sheets("Sheet")
.Range("c1:c" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=IF(B1="",TRIM(A1),TRIM(B1))"
End With
End Sub
So your sheet name is Sheet or Sheet1? And OP mentioned Sheet name is Sheet2. That removes one error. Secondly, you need to set D column as .Cells(.Rows.Count,"D").End(xlUp).Row) instead of A column.
Here is a very ugly code to try out: It takes last used row in count into the Long variable. Then set the range accordingly for setting up the formula using AutoFill.
Sub test11()
Dim l As Long
l = Sheets(1).Range("d1:d" & Sheets(1).Cells(Sheets(1).Rows.Count, "D").End(xlUp).Row).Count
With Sheets("Sheet1")
.Range("d1").Formula = "=IF(IsNull(B1),TRIM(A1),TRIM(B1))"
.Range("d1").AutoFill Destination:=Range("d1:d" & l), Type:=xlFillDefault
End With
End Sub
Your logic seems a bit strange, but this works:
Sub test11()
With Sheets("Sheet1")
.Range(.Range("c1"), .Range("C" & .Rows.Count).End(xlUp)) = "=IF(B1="""",TRIM(A1),TRIM(B1))"
End With
End Sub
You need to double the quotes within quotes in VBA.
Another variant:
Sub test12()
With Sheets("Sheet1")
Intersect(.Range("c1").CurrentRegion, .Range("C:C")).Formula = "=IF(B1="""",TRIM(A1),TRIM(B1))"
End With
End Sub

Excel macro to concatenate one row at a time to end of file

I need an Excel macro to join seven columns of data on each row until the end of the data is reached. For example if I have a formula like this:
=A1&B1&C1&D1&E1&F1&G1
How can I write the macro so that it increments for every row to the end of the file in a sequence like this?
=A1&B1&C1&D1&E1&F1&G1
=A2&B2&C2&D2&E2&F2&G2
=A3&B3&C3&D3&E3&F3&G3
With so many answers, the main focus on what assylias and I were highlighting has gone to waste :)
However, if you still want a VBA answer. Use this method. This is much faster than Looping or an Autofill.
Option Explicit
Sub Sample()
Dim LastRow As Long
Dim Ws As Worksheet
Set Ws = Sheets("Sheet1")
LastRow = Ws.Range("A" & Ws.Rows.Count).End(xlUp).Row
'~~> If your range doesn't have a header
Ws.Range("H1:H" & LastRow).Formula = "=A1&B1&C1&D1&E1&F1&G1"
'~~> If it does then
Ws.Range("H2:H" & LastRow).Formula = "=A2&B2&C2&D2&E2&F2&G2"
End Sub
If you have 1000's of rows then you might want to switch off Screenupdating and change Calculation to Manual before you run the code and then reset them at the end of the code.
I think the easiest way to do this would be to just fill down as assylias says but if you want to use VBA:
Selection.AutoFill Destination:=Range("Your Fill Range"), Type:=xlFillDefault
Should copy across the other rows.
I agree 100% with the comments and the other answers, why do you need VBA to do this, but just to answer your original question, this is how I would accomplish it:
Sub FillAllWithFormula()
Dim i As Variant
Dim wsht As Worksheet
'If you are using this for a specific Worksheet use the following
Set wsht = ThisWorkbook.Worksheets(yourWorksheetName)
'or if you are always using this for the active sheet use the following
Set wsht = ActiveSheet
For i = 1 To wsht.Rows.Count
'Replace "X" with the column letter you want your formula to appear in
wsht.Range("X" & i).Formula = "=A" & i & "&B" & i & "&C" & i & "&D" & i & "&E" & i & "&F" & i & "&G" & i
Next
Set wsht = Nothing
End Sub