Advertisement
Guest User

GarciaPL

a guest
Jul 28th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Function StrRange(ByVal nRow As Single, ByVal nCol As Single) As String
  2.         Dim sC As String
  3.         Dim nC, nRest, nDivRes As Integer
  4.        
  5.         sC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6.         nC = Len(sC)
  7.        
  8.         nRest = nCol Mod nC
  9.         nDivRes = (nCol - nRest) / nC
  10.        
  11.         If nDivRes > 0 Then StrRange = Mid(sC, nDivRes, 1)
  12.         StrRange = StrRange & Mid(sC, nRest, 1) & Format(nRow)
  13. End Function
  14.  
  15. Sub ChangeString()
  16.  
  17.     Dim range As String
  18.     range = Selection.Address(ReferenceStyle:=xlA1, _
  19.                            RowAbsolute:=False, ColumnAbsolute:=False)
  20.      
  21.     If range = "" Then
  22.         MsgBox ("The range of cells was not definedcells. Ending the macro")
  23.         Exit Sub
  24.     End If
  25.    
  26.     Dim Start
  27.     Dim Last
  28.     intPos = InStr(1, range, ":")
  29.     If intPos > 0 Then
  30.         split_string = Split(range, ":")
  31.         If UBound(split_string) = 1 Then
  32.             Start = split_string(0)
  33.             Last = split_string(1)
  34.         End If
  35.     Else
  36.         Start = range
  37.         Last = Start
  38.     End If
  39.    
  40.     Dim SheetName
  41.     SheetName = ActiveSheet.Name
  42.     If SheetName = "" Then
  43.         MsgBox ("Cannot read sheet name. Ending the macro")
  44.         Exit Sub
  45.     End If
  46.    
  47.     Dim CellAddress
  48.     For Each c In Range("" & CStr(Start) & ":" & "" & CStr(Last))
  49.         If c.Value <> "" Then
  50.             CellContent = Split(c.Value, " ")
  51.             CellAddress = StrRange(c.Row, c.Column)
  52.  
  53.             Dim wb As Workbook
  54.             Dim ws As Worksheet
  55.             Dim TxtRng  As Range
  56.             Set wb = ActiveWorkbook
  57.  
  58.             Set ws = wb.Sheets(SheetName)
  59.             Set TxtRng = ws.Range("" & CStr(CellAddress))
  60.             TxtRng.Value = CStr(CellContent(1)) & " " & CStr(CellContent(0))
  61.         End If
  62.     Next c
  63.        
  64. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement