![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
![]()
Hi, all and thanks for reading my question. I am not very good with vba but I am trying to learn. while I was practicing, I was wondering how short I could make a line of code.
So I made a word document that consist of 1 table. The table is 5 columns with 2 rows. In the fist cell on the first row I have the text "is this working". I am trying to copy the text from the first cell on row 1 to the first cell on row 2. I ran the code below. It captures the desired text but it doesn't leave (or insert, input) the text in the cell on row 2. (sorry if I am not using the correct terminology) I think maybe the T1 and T2 variables are wrong but I am not sure what they should be other than String. If someone could please let me know what I am missing I would appreciate it. Sub CopyPaste1() ' copy information from 1 cell Dim AD As Document Dim TB As Tables Dim ST As Integer Dim C1 As Cell Dim C2 As Cell Dim RG1 As Range Dim RG2 As Range Dim T1 As String Dim T2 As String Set AD = ActiveDocument Set TB = AD.Tables ST = 1 Set C1 = TB(ST).Cell(1, 1) Set C2 = TB(ST).Cell(2, 1) Set RG1 = C1.Range Set RG2 = C2.Range T1 = RG1.Text T2 = RG2.Text T2 = Left(T1, Len(T1) - TB(ST).Rows.Count) End Sub |
#2
|
|||
|
|||
![]()
I should probably use trim instead of Len, too
![]() |
#3
|
||||
|
||||
![]()
As a single line of code:
Code:
With ActiveDocument.Tables(1): .Cell(2, 1).Range.Text = Split(.Cell(1, 1).Range.Text, vbCr)(0): End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
Thanks very much for the reply, but that is not the result I was looking for (however, I do like the vbCr usage. I will have to look into that one a little deeper).
I am trying to end up with a line of code that would look something like this: T2 = Left(T1, Len(T1) - 2) Keeping all the Dim and Set statements I have. Its kinda an odd way to do it, but I'm just experimenting. Thanks for your help. |
#5
|
||||
|
||||
![]()
The conventional way of doing things would be something along the lines of:
Code:
Sub Demo() Dim Rng As Range With ActiveDocument.Tables(1) Set Rng = .Cell(1, 1).Range Rng.End = Rng.End - 1 .Cell(2, 1).Range.Text = Rng.Text End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Thanks for the information and thanks for the suggestions I will give them a try.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copying cell data from one tab to another tab | tlmy87 | Excel | 0 | 12-23-2016 07:54 AM |
Arrange shortest to longest sentence in Word | omar | Word | 8 | 11-20-2015 04:38 PM |
![]() |
mbesspiata | Excel | 1 | 04-29-2014 02:16 PM |
Copying data from one cell to another where condition is third cell | fairyca | Excel | 4 | 03-30-2014 08:22 AM |
Copying part of a cell to a new cell | paulw793 | Excel | 7 | 01-27-2012 10:46 AM |