Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following problem.
In an excel spreadsheet I am populating a calendar schedule.
Based on a drop down list, you can change the months and year, and therefore the dates adjust automatically to the week days.
Fixed week days = `E6:R6`
Calendar dates = `E7:R7`
Work Shifts = `E8:R15`
Now, when the new schedule populates and like in this case, there is no "Sunday" before October 1st, then I need to have all names from range E8:E15 removed.
I've tried an IF THEN ELSEIF in the sub, and that works, but only for the first range - E8:E15. If I switch the month to September, and we start with a Saturday, then all cells maintain their values, but E8:E15. It's like I can only do "1" IF loop and then it stops.
I'm looking for code that loops through all weeks to check if the cell shows a date or is empty. The only possible weeks with no dates would be week 1, and week 5 + 6. As you can see in the last pic, depending on how long the month is, more or less dates show.
UPDATE:
Hi there, it works now and I was able to work it out the following way.
My original code was:
If Sheets("schedule").Range("E5") = "" Then
Sheets("Schedule").Range("E8:E15").ClearContents
Elseif Sheets("schedule").Range("G5") = "" Then
Sheets("Schedule").Range("G8:G15").ClearContents
..and so on for all other ranges of my calendar.
End if
The new code is:
What I simply tried next was leaving them all separate.
If Sheets("schedule").Range("E5") = "" Then
Sheets("Schedule").Range("E8:E15").ClearContents
End if
If Sheets("schedule").Range("G5") = "" Then
Sheets("Schedule").Range("G8:G15").ClearContents
End if
.... and so on.
And now it's running just fine.
For some reason it was not able to run through several IF-loops before the end if.
Thanks everyone for all the fast help!
I'll definitely be back when I have another question about Excel and VBA :)
I'll start by assuming that range("E8:E15").clearcontents is not a typo despite being a single column while all others are 2 columns.
Code¹:
sub buildSched()
dim c as long
with worksheets("schedule")
for c = 5 to 17 step 2
if .cells(5, c).value2 = vbnullstring
.cells(8, c).resize(8, 1 + abs(cbool(c > 5))).clearcontents
end if
next c
end with
end sub
If range("E8:E15").clearcontents was a typo and you should be clearing two columns every iteration then use,
.cells(8, c).resize(8, 2).clearcontents
¹ Note code that can be copied and pasted from the page directly into a module code sheet in the VBE. Not an image of code that would require retyping to test/debug.
Related
Hi i need help on creating a VBA to copy range of cells repeatedly with one column having increments.
Current data
Expected Output
I found a vba but will only copy the rows based on column C without increments on the date
Excel VBA automation - copy row "x" number of times based on cell value
I'm not sure why you would need to do this as excel has built-pattern pattern recognition for these scenarios, after entering two succesive dates if you hover over and click the border of the cell then drag down, the desired date range will appear in the column automatically.
If you still insist on doing this programatically for whatever reason then your question already has multiply feasible solutions here: Add one day to date in cells using VBA
Specify each cell in turn then increment the value by the corresponding row number to return desired date range in column A:
Range("A2").Value = Range("A2").Value + 2 ' add 2 days
Range("A3").Value = Range("A3").Value + 3 ' add 3 days
Range("A4").Value = Range("A4").Value + 4 ' add 4 days
'-------- So on and so forth until desired range is acheived --------'
or alternatively:
Range("A2").value = DateAdd("d", 2, CDate(Range("A2")))
Range("A3").value = DateAdd("d", 3, CDate(Range("A3")))
Speaking as someone who had to learn the hard way, please take my advice and ensure you research your problem thouroughly to find a solution before posting. Refer to the guidelines here if needed.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone please help me with finding the absolute row number of nth element after filter is applied in an excel table.
For example, I have filter on and have a visible range of data element. Now 20th (nth) row in this filtered range could be 60th row (absolute sense) when no filters are on. Is there a way to find the absolute row number using VBA?
Simplest method is special cells. See below:
Sub test()
Dim i As Integer, j As Integer, k As Integer
Dim Report As Worksheet
Set Report = Excel.ActiveSheet ' Store the current worksheet in a variable (always a good idea)
Dim visRng As Range ' Creating a range variable to store our table, excluding any rows that are filtered out.
Set visRng = Report.UsedRange.SpecialCells(xlCellTypeVisible) ' Select only rows within the used range that are visible.
Dim r As Range
For Each r In visRng.Rows ' Loop through each row in our visible range ...
MsgBox (r.Row) ' ... and retrieve the "absolute" row number.
Next
End Sub
EDIT
Tom claims this method will not work, but I'm pretty sure it does what you ask. Example:
Here is my original test table--unfiltered so you can see what we're doing.
And then we filter a couple values somewhere in the middle of the table...
Now when we run the script I posted above, our message box will show the "absolute" row number for each unfiltered row. Results are 1,3,4,5, and 7.
As a function I suggest
Function RowNum(Target As Range) As Long
RowNum = Target.Row
End Function
use by entering in a cell =RowNum(E9). If you want the line relative to the table start and your table starts in -say- row 21, just subtract this from the result (you can use the same function to find the row of table start or course) ... e.g. =rownum(A2)-rownum($A$1) ... mind the absolute notation of table header!
If you don't like this as a function, you could use the SelectionChange event to display the row number of the selected cell in the message line (optionally depending on a "debug flag" somewhere in your sheet).
Tne non-VBA approach would be to use the CELL formula ... e.g. =CELL("row",A1)
The answer brought up by #Lopsided wont work, if there are other hidden cells after the nth entry. They would then be added to the absolute position.
This'll work, you have to change n by yourself in the script. Changing that shouldn't be to hard. If you have any questions regarding that, feel free to ask. (;
Sub absoluteRowID()
Dim RowCount, hiddenRows As Integer
'relative position n
n = 5
i = 0
Do While i < n
i = i + 1
If ThisWorkbook.Sheets(1).Rows(i).EntireRow.Hidden Then
'if there is a hidden row, position is incremented
n = n + 1
End If
'if there is no hidden row, nothing happens
Loop
MsgBox (i)
End Sub
HTH
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an excel table which is straight from a database. Its first column contain dates but are not formatted as dates. They are written as : 03/01/2014 for example.
So I'm trying to comparing values from some cells and the problem is people type dates as : 3/01/2014. When comparing the values no value is returned. I'm using the range.find() command. So I tried to change the format of the date column in the table but it didn't change at all. However if I try to edit a cell and just press enter it becomes a normal date. I wonder if there is a way I can convert all those rigid dates in the table to normal excel dates so that the comparison routine can work.
Thanks you.
Select the cells whose dates you wish to convert and run this small macro:
Sub DateSetter()
Dim r As Range, d As Date, st As String
For Each r In Selection
st = r.Text
If InStr(st, "/") <> 0 Then
ary = Split(st, "/")
d = DateSerial(CInt(ary(2)), CInt(ary(1)), CInt(ary(0)))
r.Clear
r.Value = d
r.NumberFormat = "m/d/yyyy"
End If
Next r
End Sub
It should perform the conversion, but ignore zeros, blanks, and other junk.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to calculate the standard deviation of alternative cells in a 10000 rows in excel 2007, how can i do that ? whether i need a formula or a VBA ?. Can some help me.
It's also possible with a formula. This "array formula" gives you standard deviation of every other cell starting at A1
=STDEV(IF(MOD(ROW(A1:A10000)-ROW(A1),2)=0,A1:A10000))
confirm with CTRL+SHIFT+ENTER
I would use both, the following code splits/sorts your rows and selects every second row and pastes it into another column, beginning with the first cell:
Sub splitcells()
Dim rows As Long
Dim i, j As Long
j = 0
rows = ?1?
For i = ?2? To rows Step 2
Cells(i - j, ?4?).Value = Cells(i, ?3?).Value
j = j + 1
Next
End Sub
Then you have to replace:
?1? - with the end row number of your list
?2? - with the beginning row number of your list
?3? - the column number of your list to be sorted
?4? - the column number of your destination column (the one the sorted data is sent to)
Once you have completed this, just run a normal standard deviation function on your new column and you'll have your answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently doing some data entry for a spreadsheet which contains hundreds on entries and want to automate the process, I have a good idea of what I want it to do but have little experience with Excel or VBA.
The idea behind it is that I have a code in one column and in the next column there is another code which is unique to the value in the former column. To give an example:
So for every cell that contains 123, the column next to it will be "ABC".
The sort of solution I would like is a macro that will work its way down Column A, storing the value of each cell (or something of that effect) and then working its way down to check for values that match that stored one. If a match is found, the macro will then copy the code from column B, the cell that is next to the stored cell and copy it into the cell in column B, next to the match.
EXAMPLE:
It will store the "123" value in A, work its way down Column A to find other cells matching "123" and when it finds them copy "ABC" into the column B cells next to the matches.
Hope this is easy to understand and someone can help me with coming up with a solution, would make this whole process alot easier as the spreadsheet is growing by the day and manual input is taking far to much time
Give this macro a try:
Sub FillInTheBlanks()
Dim rA As Range
Dim rB As Range
Dim r As Range, rr As Range
Dim N As Long
Dim va As Variant
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rA = Range("A1:A" & N)
Set rB = rA.Offset(0, 1).Cells.SpecialCells(xlCellTypeBlanks)
If rB Is Nothing Then Exit Sub
For Each r In rB
va = r.Offset(0, -1).Value
For Each rr In rA
If rr.Value = va And rr.Offset(0, 1) <> "" Then
r.Value = rr.Offset(0, 1).Value
End If
Next rr
Next r
End Sub