In the attached table I would like to link cell contents via VBA.
Column A contains contents that should be linked to the cells in column B until a new content is added to A.
The example in the attached table is shortened. The tables are much longer.
I have made a 'before' and an 'after' sheet.
The code I have is linked from A to B until a new value comes into A. But the VBA always takes the newest value in column B and not all the previous ones.
How can I adjust the code so that all values from B are linked to A until a new value comes into column A?
Sub Linking_columns_until_new_values()
Dim arr
Dim z As Long
Dim txt As String
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
With ActiveSheet.UsedRange.Columns(1).Resize(, 2)
.Columns(1).SpecialCells(xlCellTypeBlanks).Font.Bold = False
arr = .Value
For z = 1 To UBound(arr)
If arr(z, 1) <> "" Then
txt = arr(z, 1)
ElseIf arr(z, 2) = "" Then
arr(z, 1) = ""
Else
arr(z, 1) = txt & " " & arr(z, 2)
End If
Next
.Value = arr
End With
Next
End Sub
Here is the link to the sheet:
https://www.evernote.com/l/AGApoCGk-OJGKaHxwB2F-VCjO9uWJN299TM/
There is no need to use VBA for this:
In B1, because it's first entry, I copied manually.
My formula in B2 is =IF(B2="";"";C1&B2) and drag down
My formula in C1 is =B1 and drag down
Later on you could copy/paste formats, paste values, and then delete columns A and B. That way you would get what you got in After Sheet
Probably you can use the macro recorder to get the code and adapt it to your needs if you need VBA solution.
Hope this helps
Related
Is there a way to sum all the values of the same cell of multiple sheets (tab) which the name of each tab is date, and show the summed value in the MasterSheet?
For example:
Currently, I use this formula =SUM(INDIRECT("'*-"&MONTH(C2)&"-2020'!$E$3")) in the cell under the "Oct-2020" cell, as shown in the figure below, and I will do the same for Nov-20 and Dec-20.
As you can see, I am getting this "#REF!" error. Currently, I have two tabs that I am trying to get the "$E$3" cell values from, "13-10-2020" and "14-10-2020". However, if I only have one sheet (let's say "14-10-2020" only), I am able to get the value.
Does anyone know what is going on with my formula? Why it works when there is only one sheet (tab) to read from, but does not work when there are multiple sheets (tabs), even I used "*" to include all dates.
Please advice. Thank you in advanced
I think this should solve your problem:
Option Explicit
Sub sub_sum_up_months()
Dim wks As Worksheet
Dim nr_month As Integer, nr_year As Integer
Dim str_month As String, str_year As String
Dim c As Integer
Dim my_Sum As Double
For c = 3 To 5 ' Iterating over the columns "C" to "E"
my_Sum = 0
nr_month = month(Worksheets("MasterSheet").Cells(2, c).Value)
If nr_month < 10 Then
str_month = "0" & nr_month
Else
str_month = CStr(nr_month)
End If
nr_year = Year(Worksheets("MasterSheet").Cells(2, c).Value)
str_year = CStr(nr_year)
For Each wks In ThisWorkbook.Worksheets
If Right(wks.Name, 4) = str_year And Left(Right(wks.Name, 7), 2) = str_month Then
my_Sum = my_Sum + wks.Cells(3, 5) 'cells(3,5) is "E3"
End If
Next wks
Worksheets("MasterSheet").Cells(3, c).Value = my_Sum
Next c
End Sub
You can use the sum function.
Follow this step by step and it will work.
In your MasterSheet C3 type =Sum(
Click on sheet 13-10-2020 and cell E3.
Now press and hold shift.
Click on the last sheet that is October and press Enter.
Now you should get the sum of all E3 in October sheets.
As far as I know there is no formula to do this dynamic but I'm quite sure it can be done in VBA if the sheet names follow a pattern.
i have this worksheet called Test. In that worksheet i want to export to pdf then print cells G1 to cells H17. Next i want to print G1 to cells I17 skipping column H meaning without column H. In another word column G is kind of like the header and the columns that follow the result. So print Header column+ results column and so on until column CM. Below each printing page i want to also export to pdf column C18, column D18 and Column H18 if cells I18 to cells I27=500. I started the code but couldn't go on to finish it since i am not really versed in vba
Sub SetPrintArea()
Dim ws As Worksheet
Dim x As Long, result As Boolean
result = True
For x = 18 To 128
If Worksheets("AR-MD").Range("H" & x).Value <> ActiveSheet.Cells(8, 19).Value Then
result = False
End If
If Not result Then Exit For
Next x
Set ws = ThisWorkbook.Sheets("AR-MD")
ws.PageSetup.PrintArea = Union(ws.Range("g1:g17"), ws.Range("i1:i17"),ws.Range("Result").Address
End Sub
I want to be able to print the result condition but i dont know how to include it in the statement
Use the Union method:
ws.PageSetup.PrintArea = Union(ws.Range("g1:g17"), ws.Range("i1:i17")).Address
I have data in excel file for which filter has to be applied for each column independently but the filter condition is same. The reason for asking this is each column has that cell that meets the condition in a different row number.In table 1 I have 3 columns a,b and c.
I want to filter each columns independently with value=20 so that the result looks like table below
Try out this VBA code,
Sub matchvalues()
Dim i As Long, j As Long
Sheets.Add.Name = "newSheet"
j = InputBox("Enter the value to filter")
Rows("1:1").Copy Sheets("newSheet").Cells(1, 1)
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
If IsError(Application.Match(j, Columns(i), 0)) Then
Sheets("newSheet").Cells(2, i) = ""
Else
Sheets("newSheet").Cells(2, i) = j
End If
Next i
End Sub
This code will prompt the value that has to be filtered. Need to give that as input which will create a new sheet and output the values if present.
If you want to do this with just formulas, try the below. If the value that you are searching is in cell E1, enter the below formula in cell G2 and drag across.
=IF(ISNUMBER(MATCH($E$1,A:A,0)),$E$1,"")
You can change the values in E1 directly to see the updated result. Hope this helps.
I would like to turn Excel formulas with string functions into VBA equivalents.
There are several lines of formulas. I'm going to give two examples.
There is a copy a txt report in column A. Columns B thru O sort through the rows of A, picking out pieces using some of the following formulas:
=IF(MID(A6,60,1)="=",MID(A6,52,15),IF(MID(A6,57,1)="=",MID(A6,53,8),C1))
and
=IF(A2="","-",IF(LEFT(A2,1)="1","pg","-"))
Here's a snapshot of a test report.
I'd like to loop through the formulas and send to Sheet2 starting with A2 (due to headers) not go through the steps of copying then pasting to A1, copying columns B thru O, and then sorting out the "-".
I tried recording a macro and imputing the formulas, but I couldn't make it happen.
Sub convert()
Dim ws As Worksheet, thisRng As Range
Set ws = Sheet1
Set thisRng = Sheet1.Range("B1")' Select the cell to place data into
' Break up the formula logically first and it becomes easier to convert
' =IF (MID(A6,60,1)="=", MID(A6,52,15), else if IF(MID(A6,57,1)="=", MID(A6,53,8), else C1))
If Mid(ws.Range("A6").Value, 60, 1) = "=" Then
thisRng = Mid(ws.Range("A6").Value, 52, 15)
Else
If (Mid(ws.Range("A6").Value, 57, 1)) = "=" Then
thisRng = Mid(ws.Range("A6").Value, 53, 8)
Else
thisRng.Value = ws.Range("C1").Value
End If
End If
End Sub
I have multiple spreadsheets that I need to copy column starting at "S4", on a specifically named worksheet, and I need only the cells in that column (starting at S4 and everything below it) that contain data.
I need to copy that data and paste it into my "main" spreadsheet starting at A2 on a specific worksheet. I can do this with one spreadsheet, but the problem I'm running into I need VBA to find the last cell in column A that has a value and start pasting new data into the cell below it, etc... Otherwise, when it's looking at the other spreadsheets, it's just overwriting the data in my main spreadsheet.
You'll notice the specified range of S4:S2000 - its purpose was for a single spreadsheet, which worked fine because I never had data past 2000, but I really just need it looking for cell values and grabbing those.
This is the chunk of code where I'm having the trouble. I want it to search starting at A2 (skipping the column header), look for the last cell that has a value and paste cells with values starting at S4 on the other worksheet.
On Error Resume Next
Set wbkCS = Workbooks.Open(strCutSheetFile(i))
On Error GoTo 0
If Not wbkCS Is Nothing Then
With wbkVer.Sheets("Cutsheets")
.Range(.Cells(2,1)).End(xlUp).Row = wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Value
I had to tweak yours a little because I'm working with protected files, but this worked:
Set copyRng1 = Worksheets("Cutsheets").Range("A2")
If copyRng1 = "" Then
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Locked = False
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy Destination:=wbkVer.Worksheets("Cutsheets").Range("A2")
Else
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy Destination:=wbkVer.Worksheets("Cutsheets").Range("A" & wbkVer.Worksheets("Cutsheets").Range("A65536").End(xlUp).Row + 1)
End If
Here's an example that may push you in the right direction...
Suppose I want to aggregate my data in Sheet1 using data from S4:S2000 in all other worksheets in the workbook.
Sub CopyAndStack()
Dim wkShtIndex As Integer, copyRng As Range
Set copyRng = Worksheets(1).Range("A2")
For wkShtIndex = 2 To Worksheets.Count
If copyRng = "" Then
Worksheets(wkShtIndex).Range("S4:S2000").Copy Destination:=copyRng
Else
Worksheets(wkShtIndex).Range("S4:S2000").Copy Destination:=Range("A" & copyRng.End(xlDown).Row + 1)
End If
Next wkShtIndex
End Sub
I check if A2 is empty and if so I paste the first lot of data.
If A2 is not empty I get the next empty cell in column A and paste it there.
Here's how you can get exactly the rows that are populated in S:
Dim StrRange As String
. . .
'Get range
strRange = "S4:S" & Worksheets(wkShtIndex).UsedRange.Columns("S:S").Rows.Count
'Do something with range
Worksheets(wkShtIndex).Range(strRange)