Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2022, 02:49 PM
Steve Kunkel Steve Kunkel is offline Insert Column acting differently... Windows 10 Insert Column acting differently... Office 2019
Advanced Beginner
Insert Column acting differently...
 
Join Date: May 2019
Location: Seattle area
Posts: 81
Steve Kunkel is on a distinguished road
Default Insert Column acting differently...

Hi Folks,

This code used to work, but six months or so ago, it started doing the wrong thing...

The code:
Code:
Private Sub cmdMakeColLeft_Click()
 On Error GoTo ErrorHandler
  Dim Lstr As String, aCell As Cell
  Selection.InsertColumns
  Lstr = Me.txtColLeftText
  For Each aCell In Selection.Cells
    aCell.Range.Text = Lstr
  Next aCell
  Selection.MoveRight Unit:=wdCell
  Exit Sub
ErrorHandler:
    MsgBox "Only works from inside a table."
End Sub
A screenshot of some tables:


The code gets called from a GUI form. If I click on the top table, in the cell that has


Rater 1
Teacher

and enter "Teacher" into the txtColLeftText box, then when I execute the code, it is supposed to make the second table in the image.

A column is added to the left, and the text is enter (only) into the new column.

Lately, it has been erroneously replacing the text in the original column too (like the bottom table).

Any ideas how to fix this? Hopfully I explained the problem well.
-steve
Reply With Quote
  #2  
Old 04-12-2022, 05:42 PM
Guessed's Avatar
Guessed Guessed is offline Insert Column acting differently... Windows 10 Insert Column acting differently... Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It appears that in a regular table (where all columns align exactly) this code would have just filled one column. But if at least one row has different column width/alignment (or maybe there are merged cells) then more than one column gets the inserted text.

Can you verify that all the table cells are aligned perfectly?

PS. I would deal with the selection error a different way.
Code:
Sub aTest()
  Dim Lstr As String, aCell As Cell
  If Selection.Information(wdWithInTable) Then
    Selection.InsertColumns
    Lstr = Me.txtColLeftText
    For Each aCell In Selection.Cells
      aCell.Range.Text = Lstr
    Next aCell
    Selection.MoveRight Unit:=wdCell
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Column acting differently... Want to Insert Column and add text into entire column. Steve Kunkel Word VBA 2 04-14-2021 07:22 PM
not able to insert a column Zumba Excel 1 10-06-2017 11:27 PM
Insert Column acting differently... Insert Table To Multi Column Page mertdogan Word Tables 2 08-03-2017 03:46 PM
How to automatically insert a line after a Column Break YooperNC Word 3 01-29-2015 07:16 AM
Insert Column acting differently... How to insert ne wline within a column kesh Excel 2 12-09-2011 09:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft