I have the following macro, which takes a sparse set of data and copies the only entry from each row into the left-most column. Example of data
I am hoping someone could rewrite this macro into one that will work with the same data in LibreOffice.
Sub Macro1()
Dim rng As Range
Set rng = Selection
For Each row In rng.Rows
For Each cell In row.Cells
If cell <> "" Then
Debug.Print cell
row.Cells(1) = cell
End If
Next
Next
End Sub
Example of data
Have a look at Andrew Pitonyak's macro document. Section 6 is the area that deals with Calc macros.
Get the selected rows with code similar to section 6.9 Fill selected range with text.
Also use code similar to section 6.14 Display all data in a column.
Related
I'm a total beginner here. I saw this example online to copy data from a fixed range of a workbook to another workbook.
Could someone please guide me on how I can copy from A2 cell onward to the last active cell with data?
.
In this given example, I wanted to copy those cells highlighted in blue to another workbook. The data varies every time, hence a fixed range doesn't work for me. I try using ".UsedRange" but it copies the footer as well, which is something I don't need.
Sub Copy_Method()
Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D9").Copy _
Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End Sub
Ref: https://www.excelcampus.com/vba/copy-paste-another-workbook/
as per the posted screenshot:
the "footer" appears to be placed in column A
data fill all columns from A to E
hence you can use column B to get the last used cell row from:
Sub Copy_Method()
With Workbooks("New-Data.xlsx").Worksheets("Export")
.Range("A2:E" & .Cells(.Rows.Count, 2).End(xlUp).Row).Copy _
Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End With
End Sub
Given your particular set up, you can use the CurrentRegion property.
Sub Copy_Method()
Dim r As Range
Set r = Workbooks("New-Data.xlsx").Worksheets("Export").Range("A1").CurrentRegion
Set r = r.Offset(1).Resize(r.Rows.Count - 1) 'exclude the header
r.Copy Workbooks("Reports.xlsm").Worksheets("Data").Range("A2")
End Sub
I have two excel columns in a worksheet, consider as A(left) and B(right) and I have recorded a macro where it will calculate a formula and copy/paste it to all the right side columns till where the left side column has data. but when next time some extra data is added to the left column and when I run a macro to copy/paste then it is only considering the previous range but not extending to the newly added cells.
example : A1:A5 is left side and B1:B5 is the right side and my formula in B range which is right range calculate based on A1:A5 and my macro works fine and restricted only to B1:B5 even when I added new data like A1:A10 only copying B1:B5. what is the method I can use my macro automatically till the data range of A side column?
Better next time, you provide a screenshot of your data & also VBA code in question.
You are using a static range, while you require is dynamic range.
try this
Sub test()
Dim i As Integer
i = WorksheetFunction.CountA(Range("A:A"))
Range("B1:b" & i).Select 'instead of selecting you can provide your formula to whole range.
End Sub
For any other issue, feel free to comment
As per your description just try below.
Sub FillFormula()
Dim i As Long
i = Range("A1").End(xlDown).Row
Range("B1").FormulaR1C1 = "=RC[-1]+5"
Range("B1").AutoFill Destination:=Range("B1:B" & i)
End Sub
I am trying to automate a rolling calendar spreadsheet that tracks various metrics and charts them into a spark line. The script I would like to write would shift the selected range in the spark-lines every time it is ran.
I have done some googlefu and have tried using the offset function to no avail. This is because the data is in a predefined range defaulting to num 0 based on the formulas used to populate the spreadsheet int the first place.
excel vba : selected cells loop
https://www.excel-easy.com/vba/examples/loop-through-entire-column.html
https://support.microsoft.com/en-us/help/291308/how-to-select-cells-ranges-by-using-visual-basic-procedures-in-excel
I am stuck at incrementing the ActiveCell.SparklineGroups.Item(1).Item(1).SourceData from its current selected range to PPTracking!G8:R8 ... H8:S8 ... and so on each time the macro is ran.
This is my first time working in VBA and any help is greatly appreciated!
Sub Macro4()
Dim selectedRange As Range
Set selectedRange = PPTracking!F8:Q8
Range("E5:E6").Select
Application.CutCopyMode = False
ActiveCell.SparklineGroups.Item(1).Item(1).SourceData = "PPTracking!F8:Q8"
Range("E5:E6").Select
End Sub
You can either use Sparkline.ModifySourceData or directly change the Sparkline.SourceData property, as it looks like you are currently aiming to do.
This code will shift the SourceData 1 column to the right - from F8:Q8 to G8:R8, then to H8:S8, etc. - by using the original SourceData value as a reference within Range, which is then Offset by 1 column.
It concatenates the Parent.Name to the Address to get the full Worksheet Name and cell reference.
Sub ShiftSparklineData()
If ActiveCell.SparklineGroups.Count > 0 Then
With ActiveCell.SparklineGroups.Item(1)
.SourceData = "'" & Range(.SourceData).Parent.Name & "'!" & Range(.SourceData).Offset(, 1).Address
End With
End If
End Sub
Avoid using ActiveCell where possible though; reference the cell(s) with a sparkline using Sheets("Yoursheetname").Range("Cellreference")
I have a spreadsheet with a large amount of data in. About half the cells are merged horizontally with other cells and contain names e.g. John Doe.
Does anyone know how to write a macro to unmerge the cells while distributing the value of the cell to all the cells that were previously merged?
Cheers
Jack
EDIT: The reason I am doing this is to check to see if two adjacent cells are equal i.e. is A1 = A2. But I run into problems when either cell is merged. If anyone knows a way around this problem without separating the cells and copying the data that would be even better!
The idea I provide below is tested for Excel 2010 VBA Win7. However, being not sure I hope it should work as for Mac, too (as this is rather set of standard properties and methods of Range object). If this doesn't work please let me know to delete my answer.
This simple code will work for selected area however it's quite easy to change it to any other range. Some other comment inside the code below.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
Cell.UnMerge
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
And the picture presenting before and after result:
I was able to get the solution from KazJaw to work on a mac with one edit, changing Cell.UnMerge to
ActiveSheet.UsedRange.MergeCells = False, as provided by Ron Debruin here: http://www.rondebruin.nl/mac/mac027.htm.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
ActiveSheet.UsedRange.MergeCells = False
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
I'm trying to create a simple macro that copys the two first numbers in each cell in a column and printing them in a different column. This is in a excel-document with more than 1 worksheet. I've tried for a while to write the code, but with no prior experience, I have a hard time figuring out what to do. I know the "left()"-function is able to do this, but I don't know how I define which column to draw data from and to which column it will be printed. Any help will be greatly appreciated.
With no prior experience to writing VBA code, I am going to reccommend you stick to the formula method of doing. Honestly, even if you were familiar with VBA, you might still opt to use the formula.
A formula is put into the actual cell you want the value to be copied.
=LEFT(sourceCell, #of characters you want)
This is how it would look:
=LEFT(Sheet1!A1, 2)
Think of it as saying "this cell shall equal the first n characters in cell OO, starting from the left".
Once you are done with your formula, if you don't need it to be binded to the source anymore (if the sourceCell changes, so does the cell with the LEFT formula), you can highlight the cells, Ctrl + C to copy, then right-click and select Paste Special. Then select VALUE and hit OK and now the cells are hard-coded with the value they were showing, as if you typed it yourself.
Once you master using formulas, the next step is VBA. Don't go confusing yourself by jumping into VBA and writing code for ranges, etc. if you aren't comfortable with using =LEFT yet. One step at a time, and you'll be a pro before you know it. :)
Here is a quick sample sub to get you started:
Public Sub LeftSub()
Dim SourceRange As Range, DestinationRange As Range, i As Integer
'Define our source range as A1:A10 of Sheet1
Set SourceRange = Sheet1.Range("A1:A10")
'Define our target range where we will print.
'Note that this is expected to be of same shape as source
Set DestinationRange = Sheet1.Range("B1:B10")
'Iterate through each source cell and print left 2 bits in target cell
For i = 1 To SourceRange.Count
DestinationRange(i, 1).Value = Left(SourceRange(i, 1).Value, 2)
Next i
End Sub
How about
Sub foo()
Dim cell As Range
Dim sourceRange As Range
'//define the source column - looks for contiguous downward data from A1;
Set sourceRange = Range(Sheets("Sheet1").Range("A1"), Selection.End(xlDown))
'//iterate each cell
For Each cell In sourceRange
If IsEmpty(cell.Value) Then Exit For
'//example to place the value in corresponding row of column B in sheet 2
Sheets("Sheet2").Range("B" & cell.Row).Value = Left$(cell.Value,2)
Next
End Sub
Or an equivalent formula (in the destination cell)
=LEFT(Sheet1!A1,2)