Custom string reversal order - pair up 2 digits [closed] - vba

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 9 years ago.
Improve this question
I have Excel data file.
in cells I have data like this
480000074B26E42D
How can I Get result in other cell Like this
2DE4264B07000048
So, I want to take the last 2 digits and put them to front. Then the next 2 digits before the 2D and put it after the 2D(the one in front)
I try to use this code but I get wrong result
=RIGHT(B2,LEN(B2) -2)

I believe the other contributors have misunderstood your requirements & are simply offering advice to reverse the value provided.
With the input of 480000074B26E42D, I think your required outcome is incorrect: 2DE4264B07000084. The last two characters have been transposed, & the actual output will be:
2DE4264B07000048.
If that is correct, you may use this Visual Basic for Applications function (stored in a Public code module)
Public Function strReverse_Character_Pairs(ByVal strValue As String) As String
Dim lngLoop As Long
Dim strReturn As String
strReturn = ""
For lngLoop = Len(strValue) - 1& To 1& Step -2&
strReturn = strReturn & Mid$(strValue, lngLoop, 2)
Next lngLoop
strReverse_Character_Pairs = strReturn
End Function
Usage, based on the original text in cell A1, would be as follows (with this formula placed in any cell other than cell A1):
=strReverse_Character_Pairs(A1)
The function could also be enhanced to check that the value has an even number of characters by adding a "dummy" character at either end so that the reversed pairs are as intended.

I think a little vba may be the the answer, I just tested this and it works fine

Use the following function in VBA:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cells(3, 1) = StrReverse(Cells(1, 1))
End Sub
Where Cell A1 contains the string to be flipped. Cell A3 contains the result

Related

How to count straight cells in a table [closed]

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 1 year ago.
Improve this question
Im working with a spreadsheet which I can't format the way I think it would be easier so I'm tryng to work around.
There's a table with the Employee names and the weekdays which I have to leave it blank if he is not working that day or fill in with his specific shift time. Basically, I want to display a message if the employee will work more than 4 days straight or if he has more than 48h already assigned on that week.
I can manage to display the message if the hours are above 48, but I can't manage the 4 days straight. I tried using FOR to go all over the cells but I can't formulate a counter.
I'm pretty sure there's an easier solution for that, better than using FOR but my knowledge on this is quite low.
Table
You could use an array formula to show the longest streak of consecutive days.
=MAX(FREQUENCY(IF(LEN(B2:H2)>0,COLUMN(B2:H2)),IF(LEN(B2:H2)>0,0,COLUMN(B2:H2))))
Enter that with Ctrl+Shift+Enter, not just Enter. You can compare the result of that formula to "4" and if it's greater, do whatever you want.
If you can't (or don't want to) add a formula, here's a VBA function that does the same thing.
Public Function MaxConsecutiveDays(ByRef rRng As Range) As Long
Dim rCell As Range
Dim lCnt As Long
Dim lMax As Long
For Each rCell In rRng.Cells
If IsEmpty(rCell.Value) Then
lCnt = 0
Else
lCnt = lCnt + 1
End If
If lCnt > lMax Then
lMax = lCnt
End If
Next rCell
MaxConsecutiveDays = lMax
End Function
Then you can use that in your code like
Public Sub test()
If MaxConsecutiveDays(Sheet1.Range("B2:H2")) > 4 Then
Err.Raise 9999, , "Too many days"
Else
MsgBox "OK"
End If
End Sub
All of this assumes that you if there is anything in a cell, it's 8 hours and that you don't have to concern yourself with parsing the hours string in the cell to determine how long they're scheduled for.

Split cell content by "';" using VBA or Excel formula [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to VBA, so I have trouble writing code in VBA. My problem is writing a code or an Excel formula to extract (for later use) the identifiers that are written in the same cell, split by ";".
For example, this is what I have on a cell:
A11;B22;C33
I need to select A11, B22, C33, as they were written for example, on a column, as different cells, so I can later use them in other formulas.
I don't want to split the content into different columns, I just want to be able to use A11 or B11 furthermore, without editing it in the databse
Thank you!
you can use Split function and pass delimiter (;in your case)
for Example:
Var arrayResult = Split("A11;B22;C33",";")
you can also use text to column utility in excel.
select the text you want to separate
go to DATA >
select text to column>
select delimited option click Next>
select your favorite delimiter ;
click next
you will get the separate values.
I believe the following should work as you expect it to, simply amend the worksheet you are using, the cell you are referring to and the Column you wish your results to be placed in:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with
Dim newvar As Variant, var As String
var = ws.Range("A1").Value
'get the contents of your desired cell, in this case A1
newvar = Split(var, ";") 'split the cell contents by delimiter ; into an array
For i = LBound(newvar) To UBound(newvar) 'loop through array to place values into column B
ws.Cells(i + 1, "B").Value = newvar(i)
Next i
End Sub

Excel VBA How to multiply range by one number [closed]

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 7 years ago.
Improve this question
I am very new to VBA and I am seeking help to solve the following problem. I need to multiply a range by a single number. All of the cells with in that range need to be multiplied by that one number.
Thank you!
As what I comment just now.
1.Enter the formula at the cells (e.g. G1)
2.Press Enter key and drag the formula
keong has already shown you one method but unfortunately that method requires one to do the calculation in another cell/range. If that is what you want then go with keong's answer but if you want to do the calculation in the same range then continue reading below.
Here is another method which doesn't use formulas or VBA.
Let's say the range is A1:A10 and you want to multiply the entire range by 5
Simply type 5 in any blank cell. You can delete that later.
Copy that cell
Select Range A1:A10
Right click on it
Click on Paste Special | Values - Multiply as shown below and you are done.
Before
After
Followup from comments
In case you do not want to use a temp cell to write 5 then you can directly set 5 in the clipboard and use it like this.
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim MyData As New DataObject
Dim rng As Range
Set ws = Sheet1
Set rng = ws.Range("A1:A5")
'~~> Put the number 5 in clipboard
MyData.SetText 5
MyData.PutInClipboard
'~~> Get the data from clipboard
MyData.GetFromClipboard
rng.Formula = Application.Evaluate("=" & _
rng.Address & _
"*" & _
Val(MyData.GetText))
End Sub
Like I mentioned, you don't need VBA for this but if you still want to use VBA then you can use this instead of copying the data to the clipboard.
rng.Formula = Application.Evaluate("=" & rng.Address & "*" & MYNUMBER)
Where MYNUMBER is the variable which has the number that you want to multiply the range with.

Conditionally copy rows to different sheet if leftmost cell has ANY value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
New to the site; would greatly appreciate your help!
I want to conditionally copy rows from "sheet1" to "sheet2".
Condition: The leftmost cell in the row (column "A") has ANY value (numbers or text).
*I do not want to copy rows that are blank.
I'd like to run the VBA macro for all of sheet1(specifically columns A through L and rows 10 - 9999).
If you guys need any more info, please let me know; Thanks in advance!!
Without writing your code for you, this will get you started:
Private Sub Find_Non_Blanks()
Dim cell As Range, rowOut As Long
rowOut = 0
For Each cell In Sheets("Sheet1").Range("A10", "A9999")
If Not IsEmpty(cell) Then
rowOut = rowOut + 1
Debug.Print rowOut & ":", "A" & cell.Row, cell.Value, cell.Formula
End If
Next
Debug.Print "Found " & rowOut & " rows to be copied."
End Sub
You can put this code on any sheet, userform or module. It will produce the same output regardless of which sheet is currently active, or what the current selection is.
For speed, reliability and ease-of-maintenance, your VBA should operate directly on ranges without selecting or activating them. In other words, try hard not to use these keywords in your VBA code:
Select, Selection, Activate, ActiveCell

How to search for an item and select it in VBA [closed]

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 would like to search for a value in a cell in a spreadsheet and select it. Here is some pseudocode for what I want to do:
search for the unique value "Justin123" in spreadsheet
select the cell that contains the value "Justin123"
offset 2 cells to the left of the searched value and replace with new value "John123"
I tried to google a solution but all I found were recommendations on using the record macro feature to search and replace an item. However, what I need to do is slightly different since I need to search and replace the item two units to the left of the searched cell.
See if you understand this code (hope it gets you interested in learning VBA):
Sub TestReplace()
ActiveSheet.Cells.Find(What:="Justin123", LookAt:=xlPart).Offset(0,-2).Value = "John123"
End Sub
The Macro Recorder is a great tool for beginners. Please do use it for simple cells operations.
Basically, what you have to do is initialise a RANGE object (which represents a cell in excel). Then you set the range object to the output of the function .Find(), which returns a range object from the sheet that matches the search inputs. If there is no match, it sets the range object to Nothing. You can then use the method .Offset() that range objects have to traverse across the other range objects in the sheet.
Sub Main()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:= "Justin123", LookAt:= xlPart)
' Check if rng is not Nothing [ie a match was made using .Find()]
If Not rng Is Nothing Then
rng.Offset(0, -2).Value = "John123"
End If
End Sub